awk


set the variable in the command line. for example:

      # example.awk file
      BEGIN    { FS="|"; OFS="/"; }
      $1 ~ 1   { myvar++;     }
      END      { print myvar; }

 $> awk -v myvar=100 -f example.awk example.dat 
 

the

    if, while, do...while, for, break, continue 
statements are based on the ISO C standard

NR
the ordinal number of the current record from the start of input
inside a BEGIN action the value shall be zero
inside an END action the value shall be the number of the last record processed

There is one more common and useful pattern. It is the comma separated pattern. A common example has the form:

    /start/,/stop/ {print}
This form defines, in one line, the condition to turn the action on, and the condition to turn the action off. That is, when a line containing "start" is seen, it is printed. Every line afterwards is also printed, until a line containing "stop" is seen. This one is also printed, but the line after, and all following lines, are not printed.

The conditions do not have to be regular expressions. Relational tests can also be used. The following prints all lines between 20 and 40:

   (NR==20),(NR==40) {print}

You can mix relational and containment tests.

   Special Variables
   Variable    Purpose 
   ..........................................
   FS          Field separator   
   NF          Number of Fields    
   RS          Record separator    
   NR          Number of input records   
   FILENAME    Current filename    
   OFS         Output field separator    
   ORS         Output record separator   
   ARGC        # of arguments     
   ARGV        Array of arguments     
   FNR         Input record number    
   OFMT        Ouput format (default "%.6g")    
   RSTART      Index of first character after match()     
   RLENGTH     Length of string after match()     

built-in functions

int(expr) - truncate to integer

substr($n,k,t) - takes field n and trancate it from k char with lenght t, discarding all others characters

gensub(r, s, h [,t]) - Search the target string t for matches of the regular expression r. If h is a string beginning with g or G, then replace all matches of r with s. Otherwise, h is a number indicating which match of r to replace. If t is not supplied, use $0 instead. Unlike sub() and gsub(), the modified string is returned as the result of the function, and the original target string is not changed.

gsub(r, s [,t]) - For each substring matching the regular expression r in the string t, substitute the string s, and return the number of substitutions. If t is not supplied, use $0.

sub(r, s [,t]) - Just like gsub(), but replace only the first matching substring. Return either zero or one.