Posts

Showing posts from July, 2013

Tested & verified 50 useful AWK One Liners

1. How to print the line which matches /regex/?    awk '/regex/{print $0}' 2. How to print the line immediately above the line containing /regex/? awk '{x=y"\n"$0;y=$0};x~/regex/{gsub(/\n.*/,"",x);print x}' 3. How to print the line immediately after the line containing /regex/? awk ' {x=NR+1};NR==x{print $0}' 4. How to get the uniq lines from a file & the count the line is repeated? awk '{arr[$0]++;};END { for (i in arr) { print i "\t" arr[i];} }' 5. Precede each lines with its line number. awk ‘{print NR “\t” $0}’ 6. Precede line numbers only on the non-blank lines. awk 'NF{++c; print  c "t" $0};NF==0{print $0}' 7. Count the Lines. awk ‘END{print NR}’ 8. Printing words of a line in reverse order awk '{for (i=NF;i>0;i--){ printf  "%s%s",$i,FS  } print "" }' 9. Printing lines of a file in Reverse order. awk ‘{ arr[NR]=$0...