Tag: programming
Perform Satistical Operations on Columns in CSV Files
by lokety on Mar.07, 2007, under Posts
This is a simple but powerful way to process files in Unix, using the humble program awk.
To calculate sum or average of a numerical column of a comma separated file, create a text file like so:
{ s += $3 }
END { printf “sum = %.2f, avg = %.2f, hits = %d\n”, s, s/NR, NR }
Use your creative juices to save it with a meaningful name, say, test.awk.
Call awk with this file, like so:
You should see the sum, average and number of lines processed. In this example, it is assumed that the values in each line are separated by commas, and the numerical column is the third one.
For more information on awk, RTFM or Google it.
Technorati Tags: programming, unix, awk, comma separated values, csv, sum, average, statistical
Print Unix File Timestamp Accurate Down to Seconds
by lokety on Jan.31, 2007, under Posts
As most of us know and need, ls -l shows us detailed information about files in a directory.
total 32
-rw-r–r– 1 bin bin 1668 Mar 5 2004 mozicon16.xpm
-rw-r–r– 1 bin bin 2944 Mar 5 2004 mozicon50.xpm
Here is a single perl command that can print the timestamp of files with full date and time, accurate down to the seconds:
Replace the asterisk at the end of the command with any filename or wildcard expression you require.
Technorati Tags: unix, perl, ls, timestamp, seconds, date and time, unix file
Print Yesterday’s Date Using Perl
by lokety on Dec.22, 2006, under Posts
Quick tip for working with Unix scripts that need yesterday’s date. If you have Perl, try this:
should print to stdout the date in the yyyymmdd format. Much simpler than writing an entire function in a script and messing around with 1st of the month, leap years, and possibly dates beyond 2037 (check Perl documentation).
Links:
The UNIX Forums – get yesterday in yyyymmdd format
Technorati Tags: unix, programming, script, perl, yesterday’s date