Quick Guide to nawk

Here is a quick guide to nawk. I prefer to use nawk over awk as it has more functionalities. Most systems now would have both programs installed. See also:

Quick guide to GNUPlot
Quick guide to Docker
Quick guide to CVS
Quick guide to Emacs
Quick guide to Git

To run nawk

  • From command line : nawk ‘program’ inputfile1 inputfile2 …
  • From a file : nawk -f programfile inputfile1 inputfile2 …

Structure of nawk program

  • A nawk program can consist of three sections: nawk ‘BEGIN{…}{… /* BODY */ …}{END}’ inputfile
  • Both ‘BEGIN’ and ‘END’ blocks are optional and are executed only once.
  • The body is executed for each line in the input file.

Field Separators

  • The following example adds the field ‘=’ separator, in addition to the blank space separator: nawk ‘BEGIN{FS = ” *|=”}{print $2}’ input file.
  • For example, if the input file contains the line “Total = 500”, then the output will be 500.

Printing Environment Variables

  • The following example appends the current path to a list of filenames/directories:
    ls -alg | nawk ‘{print ENVIRON[“$PWD”] “/” $8}’
  • ENVIRON is an array of environment variables indexed by the individual variable name.
  • The variable FILENAME is a string that stores the current name of the file nawk is parsing.

Examples of usage

  • To kill all the jobs of the current user : kill -9 `ps -ef | grep $LOGNAME | nawk ‘{print $2}’`

Multi-dimensional array

  • To use 2D or multi-dimensional array, use comma to seperate the array index: matrix[3, 5] = $(i+5)

Another examples

  • The example below calculates the averages for 16 items from 10 sets of readings.
  • Example of an input line the program is trying to match: Total elapsed time is 560
BEGIN{
  printf("--------- Execution Time -----------\n");
  item=16;
  set=10;
}
{# all new variables are initialized to 0for(;j < set;j++)
  for(i=0;i < item; i++)
  {# skip input until the second word matches "elapsed"while($2 != "elapsed")
  getline;# notice the use of array without declaring its# dimensionsum[i]+=$5;
getline;
  }

if(j==set){for(i=0;i < item;i++){
   
  # this and the next 2 lines are comments
  # you can use either print or printf for output 
  # print sum[i]/set;
   
  printf("Set %d : %6.3f\n",i,sum[i]/set);
}
j++;
  }
}END{
  printf("-------------- End --------------");
}

Examples from the man page

  • Write to the standard output all input lines for which field 3 is greater than 5:
    $3 > 5
  • Write every tenth line:
    (NR % 10) == 0
  • Write any line with a substring matching the regular expression:
    /(G|D)(2[0-9][[:alpha:]]*)/
  • Print any line with a substring containing a G or D, followed by a sequence of digits and characters:
    /(G|D)([[:digit:][:alpha:]]*)/
  • Write any line in which the second field contains a backslash:
    $2 ~ /\\/
  • Write any line in which the second field contains a backslash (alternate method). Note that backslash escapes are interpreted twice, once in lexical processing of the string and once in processing the regular expression.
    $2 ~ “\\\\”
  • Write the second to the last and the last field in each line, separating the fields by a colon:
    {OFS=”:”;print $(NF-1), $NF}
  • Write lines longer than 72 characters:
    {length($0) > 72}
  • Write the first two fields in opposite order separated by the OFS:
    { print $2, $1 }
  • Same, with input fields separated by comma or space and tab characters, or both:
    BEGIN { FS = “,[\t]*|[\t]+” }{ print $2, $1 }
  • Add up first column, print sum and average:
    {s += $1 }END{print “sum is “, s, ” average is”, s/NR}
  • Write fields in reverse order, one per line (many lines out for each line in):
    { for (i = NF; i > 0; –i) print $i }
  • Write all lines between occurrences of the strings “start” and “stop”:
    /start/, /stop/
  • Write all lines whose first field is different from the previous one:
    $1 != prev { print; prev = $1 }
  • Simulate the echo command:
    BEGIN { for (i = 1; i < ARGC; ++i) printf “%s%s”, ARGV[i], i==ARGC-1?”\n”:””}
  • Write the path prefixes contained in the PATH environment variable, one per line:
    BEGIN{n = split (ENVIRON[“PATH”], path, “:”) for (i = 1; i <= n; ++i) print path[i]}

Quick Guide to Emacs

Here is a quick guide to emacs. These are a collection of commonly used keystrokes. See also:

Quick guide to GNUPlot
Quick guide to Docker
Quick guide to nawk
Quick guide to CVS
Quick guide to Git

Key Definitions

  • Ca-Cb : Press [Control][a] follow by [Control][b].
  • Ma-b : Press [Esc][a] follow by [b].
  • F1-t : Press function key [F1] follow by [t].

Tutorial

  • Please go through the Emacs tutorial If you haven’t done so: Ch-t or F1-t

Useful things to put in .emacs

  • Show line number : (line-number-mode 1)
  • Show column number : (column-number-mode 1)
  • Show time : (display-time)
  • Change color theme: (load-theme ‘deeper-blue t)
  • The list of default color themes besides “deepter-blue” includes:
    • adwaita
    • deeper-blue
    • dichromacy
    • leuven
    • light-blue
    • manoj-dark
    • misterioso
    • tango
    • tango-dark
    • tsdh-dark
    • tsdh-light
    • wheatgrass
    • whiteboard
    • wombat

Navigations

  • Quit : Cx-Cc
  • Move between windows : Cx-o
  • Goto a line : Mg

Editing

  • Cut : Ck
  • Paste : Cy
  • Select/mark region : C[space]
  • Exchange mark and point : CxCx
  • Cut rectangle region : Cx-Rk
  • Paste rectangle region : Cx-Ry

Search

  • Quick search forward : Cs
  • Quick search backward : Cr

Keystroke Record/Playback

  • Record : Cx-(
  • Playback : Cx-)

Various Different useful modes

You can enhance XEmacs with various useful features. The following two packages can be installed with minimal effort. Email me if you require any help in installing them.

  • Flyspell : On-the-fly spelling checker.
  • X-Symbol : Enables the showing of superscript, subscript and greek characters in LaTex.

To check for grammar errors in Latex, the best way is to convert your Latex document to HTML and use one of the online Grammar checkers.

Quick Guide to CVS

Here is a quick guide to setting up and using CVS. The commands shown below was tested on Concurrent Versions System (CVS) 1.10 available from Cyclic Software.

See also:

Setup

  • Set CVSROOT to a directory (e.g. setenv CVSROOT ~/cvsroot )
  • Run “cvs init”

Put Project into CVS

  • To add version control for a project consist of multiple files in a directory, e.g. ~/project
  • cd to ~/project
  • Run “cvs import -m “Project” myproject sample start”
  • The respective arguments are :
  • cvs -m “Log message” repository vendor-tag release-tags
  • You can put anything you like for the tags and log message.
  • You will need to use “repository” to checkout your files later.

Checkout a Project

  • To checkout a project
  • Run “cvs checkout myproject”
  • You can subsitute “myproject” for any other repositories in the CVS.

Update a Project

  • To update a project after modifying several files
  • Run “cvs commit”
  • An editor will be shown to allow you to add comments on the changes
  • To use another editor eg: xemacs, do “setenv EDITOR xemacs”

Add/Remove File

  • To add a file, run “cvs add filename; cvs commit”
  • To remove a file, run “cvs remove filename; cvs commit”

Topics not covered here

  • Version branches
  • Multiple developers

See also this site for a more comprehensive tutorial.