Thursday, 19 April 2007

awk

what does awk do:

  1. deal with text file which is thought of as being made up of records and fields. we can write programing by awk to do anything we want.
  2. we can:execute UNIX command from within a script; process the output from UNIX commands; work with multiple input streams; define functions.
  3. we can combined with sed and shell scripting. compared: shell is very easy and quick to write, but lacks functionality. sed, awk, and shell are designed to be integrated.

invoke:

  1. awk [options] 'script' var=value file(s)
  2. awk [options] -f scriptfile var=value file(s) : we can have more than one -f scriptfile.
assigning values to variables can be:

  1. a literal: awk -f scriptFile count=5
  2. a shell variable: awk -f scriptFile count=$count
  3. A command substitution: awk -f scriptFile count=`who wc-l`

set filed seperator:

  1. using -F
  2. one line can be sperated into different filed and can be refered by variables $1, $2 etc. $0 means the whole record.

awk script consist of patterns and procedure:

  1. a pattern can be : /regular expression/, eg /^...:/ or relational expression, eg $1=="gdm". ~ means match, !~ means don't match, eg awk -F: '$1 ~ /.dm.*/ {print $0}' /etc/passwd. BEGIN specifies procedure to take place before the first input line is processed, END as well, eg awk ‘BEGIN {print “Version 1.0”}’ dataFile. pattern missing, the procedure applies all lines
  2. procedure is arround by {} and seperated by newlines or semi-colons. can be: commands, functions and veriable assignment. procedure missing, printed all lines matched by pattern.

commands used with procedures: 5 types

  1. Assignments of variables or arrays
  2. Commands that print
  3. Built-in functions
  4. Control-flow commands
  5. User-defined functions (in nawk only)


awk Built-in Variables:

  1. FILENAME - current filename
  2. FS - Field separator
  3. NF - Number of fields in current record
  4. NR - Number of current record
  5. RS - Record separator
  6. $0 - Entire input record
  7. $n - nth field in current record

awk operators: $_++_ --_ +_-_!_*_/_%_<_<=_>=_>_!=_==_~_!~_in_$$__?:_=_+=_-+_*=_/=_%=

two ways to usingarrays in awk:

  1. in an assignment statement: myArray[i]++
  2. split() function : n=splict(input,words," ") : splict this input by " "(whitespace) into words.

functions in awk: works in the same way as java: calling a function, no sapce between function name and left bracket.

No comments: