Monday, August 30, 2010

The Three UNIX File Times

access time - cat myfile # read
ls -Elu

modification time - echo "Hello, World!" > myfile # writing
ls -El

change time - chmod a+w myfile # inode's update
ls -Elc

Friday, August 27, 2010

Counting lines in a file (61960627 lines)

sed -n '$=' file.txt

real    1m9.237s
user    1m8.602s
sys     0m0.631s

perl -ne 'END { print $NR }' file.txt

real    0m13.876s
user    0m13.245s
sys     0m0.630s

awk 'END { print NR }' file.txt

real    0m8.866s
user    0m8.257s
sys     0m0.608s

wc -l file.txt

real    0m2.550s
user    0m1.677s
sys     0m0.873s

wc file.txt

real    3m4.875s
user    3m3.970s
sys     0m0.895s

Friday, August 6, 2010

Padding a number to the right with zeros in Perl

#!/usr/bin/perl

use strict;
use warnings;

while (<>) {

chomp;

if (/^[a-zA-Z]/) {
print "$_\n";
next;
}

my ($Date, $Time, $Price, $Volume, $Index) =
split /,/;

$Time = sprintf("%0*d", 6, $Time);

print "$Date,$Time,$Price,$Volume,$Index\n";

}

__END_