Archive

Archive for the ‘awk’ Category

how to print single quote in awk

July 4, 2012 Comments off

print “\x27”

x27 is the hexadecimal value of single quote.

Advertisements
Categories: awk, Technical

Split function in awk

Syntax:
======
     count = split(string,array,delimiter);
Example
======
     splittext=”This is a sentence”

     count_items = split(splittext,splitarray, ” “);

output:
=====

     splitarray[1]=”This”
     splitarray[2]=”is”
     splitarray[3]=”a”
     splitarray[4]=”sentence”

Loop thru the result:
===============

     for(i=1;i<count_items;i++)
     {
           print splitarray[i]
     }

Categories: awk, Technical

Find substring in string/Index of substring in string using awk

Syntax:
======
    index(string,substr)
if result of index(string,substr) is zero then substring is not found
else returns the left most position of the substring.

Example:
======
    index(“This is a string”,”is a”)

Output:
======
     6

Categories: awk, Technical

Trim leading and trailing spaces using awk

June 18, 2010 Comments off

Ans:
s=” This is a text with leading and training space ”
gsub(“^[[:space:]]*”, “”,s);
gsub(“[[:space:]]*$”, “”,s);

Ouput:This is a text with leading and training space

Categories: awk, Technical

Replace double quotes in a file using awk

Ans:cat filename | awk ‘{gsub(/\x22/, “\\\x22”,s);}’ > filename

Or

cat filename | awk ‘{gsub(/\x22/, “replace string”,s);}’ >filename

here \x22 is the hexa value of double quote

Categories: awk, Technical

Time in Awk

How to print time in awk?

Ans: Using strftime()

Example: print  strftime(“[%a %b %d %H:%M:%S]”) Will print

[Wed Jun 02 12:12:12 ]

Categories: awk, Technical