awk

Ok, how is awk an underdog in scientific computing. Well, it is very well suited for performing simple tasks on data files.

E.g., you may run

awk ‘{print $1-45,$2/1800*0.82}’ intr.dat

To shift the x value from the first column of the data file, and scale the y value from the second. Or anything one could imagine. The result can be put in another file ( ” … > result.dat “), or piped into another program, e.g. xmgrace:

awk ‘{print $1-45,$2/1800*0.82}’ intr.dat > xmgrace -pipe

Script files are also helpful to perform more complex operations on data files, like the following example, that prints just certain lines of a file:

BEGIN{
nn=1000
}

(NR==1) { nn=$1+2 }

{ if ((NR-1)%nn==0) { print $1 }
else if ((NR-1)%nn!=nn-1) {
print $1, 1*$4
}
}

END{
print nn
}

Update

awk + gnuplot together!!!

Look at this beautiful command that plots a data file, plus the data in line 2:

plot ‘data.dat’ w l, “< awk ‘(NR==2) {print $0}’ data.dat “

The command must be executed from gnuplot. It works by “piping” the output of the awk command directly into gnuplot.

One thought on “awk

  1. Pingback: Emacs in scientic computing « Daniel Duque Campayo

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s