Friday, September 13, 2013

Awk trick

Awk trick 2


>>-- 13 September 2013 --<<

To get the grep -A and Grep -B in the Aix box.

nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=2 a=4 s="string" file1

where "b" and "a" are the number of lines to print before and after string "s".


Thanks
~ aks

Thursday, September 12, 2013

Awk trick

Awk trick 1


>>-- 12 September 2013 --<<

To to search a pattern of 31 and 39 in a big file at 155 to 157 position, and need to save the results in separate files with search pattern name

Use below comment

awk '{p=substr($0,155,2)} p ~ "3[19]" {print > p ".txt"}' file


Thanks
~ aks