Home > Technical > Simple awk

Simple awk

To replace open “(” or close “)” in awk

 gsub(/\(/, “you string”,variable name);
 gsub(/\)/, “your string”,variablename);

To Replace single quote :

                                  gsub(/47/, “replacement string”, variable);

                 047- is the octal value of single quote

To Concatenate string:

variable3 = variable1 variable2;

To Split and put it in an array:

                    n=split(str,strarr,”,”);

                   n— Number of elements or size of strarr

                  “,” — Delimiter

To Substring :

resultString = substr(“your string here”,starting position,Ending position);

Example :

                       resultString=substr(“your string here”,1,5);

Length of a variable:

              len=length(variable);

Comments in awk:

                      #Whatever comes after this pound symbol will be comments in awk

For Loop :

                           for(i=1;i<n;i++)  {

                                            #your statements here

                              }

While Loop:

                        while(your condition)

                          {

                                           # your statements here

                           }

if Condition :

              Simple if = if(your condition) { }

             Simple  if else  = if(your condition) {} else {}

                else if = if(your condition) {} else if(your alternative condition) else {}

                Operaters for if conditions :   == (equal to) , &&(And) , || (OR) , !(Not) , <= (less than or equal) , >=(Greater than or equal) , <(less than), >(Greater than)

Passing parameter to AWK:

                  awk -F” ” -vYour_Param=”Value) {}

                -F  — Field Separator

               -v—-Using this option you can send your parameter to awk

Advertisements
Categories: Technical
  1. May 28, 2010 at 5:54 pm

    If only I had a nickel for each time I came to karuvachi4kavithaigal.wordpress.com… Superb post!

  2. June 2, 2010 at 4:02 am

    Thanks for your visit …and encouragement !!!!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: