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.

Friday, December 12, 2014

SCRIPTING:: Linux sed tool

sed is Stream Editor and a fabulous stram editor. It can do wonders for linux programmers working in scripting domain. For the professionals in semiconductor industry, it's an elixir at times.

Some of the most common things that can be done are as follows -

1./    s/..../..../ To Substitute


To replace all instance of black with white:

$ sed s/black/white/ new

There's some particular need to replace the output of a previous command in script. 

   #! /bin/csh -f

   foreach word ( `/bin/ls *.do` )
        sed 's/$1/$2/g' $word > /tmp/a
        \cp -rf /tmp/a $word
   end