Tuesday, December 23, 2014

AWK - A tool for almost everything....

AWK

Some very good links I want to keep at fingers -

Awk Introduction Tutorial – 7 Awk Print Examples

http://www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/

Awk Tutorial: Understand Awk Variables with 3 Practical Examples

http://www.thegeekstuff.com/2010/01/awk-tutorial-understand-awk-variables-with-3-practical-examples/

8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR



http://www.thegeekstuff.com/2010/01/8-powerful-awk-built-in-variables-fs-ofs-rs-ors-nr-nf-filename-fnr/

Basics of AWK -

awk program consists of :
  •  An optional BEGIN segment
    • For processing to execute prior to reading input
    • pattern - action pairs
  • Processing for input data
    • For each pattern matched, the corresponding action is taken
  • An optional END segment
    • Processing after end of input data
BEGIN {action}
        pattern {action}
        pattern {action}
        .
        .
        .
        pattern { action}
END {action}

Examples

Some of the basic usage for AWK :

  • In order to extract column of a file - AWK can seperate out the columns of a file.
         $ awk '{ print $1}'

         The above command prints first column out of file.

         $ awk '{print $1, $2}'

         The above command in addition prints second column too.

         For the formatted printing, printf can be used as follows -

         $ awk '{ printf "first column %s second column %s", $1, $2}'

         The above command would print the formatted string as specified.

No comments: