Help Topic: Basic Usage Spring 2018 Michael Benjamin, [email protected] Department of Mechanical Engineering, CSAIL MIT, Cambridge MA 02139

Using awk

Basic awk usage awk is a powerful command line tool for manipulating files. awk is specified by POSIX, so if you have a based OS, you have awk. You can check:

which awk

/usr/bin/awk

awk operates over the lines of its input (either a file or stdin). A useful awk command is print:

awk ’END {print "Hello, world"}’

This awk program does nothing until reaching the END of its input. Since no file is provided, awk is reading from stdin and will do nothing until you EOF with Ctrl-D.

Hello, world

Let’s look at how awk works when it’s actually reading a file:

printf "first line\nsecond line" > foo.txt awk ’{print $0}’ foo.txt

first line second line

The $0 macro means ”the entire line”. Alternatively, you an access ”fields” using $1, $2, etc. awk separates the input into fields based on the value of the variable FS. The default value of FS is a space.

awk ’{print $2, $1}’ foo.txt

line first line second

Extracting Data with awk

The awk primary utility in MOOS is to extract logged data from an alog file. aloggrep extracts individual variables from the file, but keeps additional information that interferes with piping the logged values to other programs.

1 aloggrep -q short.alog NAV_X

350.163 NAV_X iM200 130.35441 350.363 NAV_X iM200 130.41986 350.666 NAV_X iM200 130.48531 350.967 NAV_X iM200 130.64412 351.168 NAV_X iM200 130.70957 351.348 NAV_X iM200 130.74666 351.586 NAV_X iM200 130.79748 351.973 NAV_X iM200 130.89911 352.175 NAV_X iM200 130.92202 352.376 NAV_X iM200 130.93121 352.679 NAV_X iM200 130.93994 352.982 NAV_X iM200 130.95740 The second and third columns (the variable and the publishing App) are not desired (and in the first column may not be desired either). To export the x position against time to other utilities, we can pipe aloggrep to awk:

aloggrep -q short.alog NAV_X|awk ’{print $1, $4}’

350.163 130.35441 350.363 130.41986 350.666 130.48531 350.967 130.64412 351.168 130.70957 351.348 130.74666 351.586 130.79748 351.973 130.89911 352.175 130.92202 352.376 130.93121 352.679 130.93994 352.982 130.95740

Using gnuplot gnuplot is a command line plotting program. It should be available through your pacakge manager. More information can be found at the homepage, http://www.gnuplot.info/. Let’s try a simple gnuplot example. First, create a dummy file, then invoke gnuplot interactively.

printf "0 0\n1 1\n2 2\n3 3\n" > foo.dat gnuplot

From the interactive gnuplot prompt, you can invoke any gnuplot command. This basic plot command will plot the data we stored in foo.dat.

plot ’foo.dat’

2 Now close the plot and quit gnuplot with Ctrl-D. gnuplot will accept data on stdin when told to plot ’-’.

gnuplot -e "plot ’-’"

Now type the same data (0 0 . . . 3 3) followed by e (when stdin is a instead of a , gnuplot keeps reading from stdin until it sees e).

0 0 1 1 2 2 3 3 e

Combining aloggrep, awk, and gnuplot The ability to plot from the command line makes it very easy to visualize alog data. Lets plot the X data that we extracted from aloggrep using awk before.

aloggrep -q short.alog NAV_X|awk ’{print $1, $4}’|gnuplot -e "plot ’-’"

3 4