awk ‘BEGIN{action} pattern{action} END{action}’
1. double space a line
awk '1; {print ""}'
awk '{print; print ""}'
awk 'BEGIN{ORS="\n\n"}; 1'
awk 'NF{print $0 "\n"}' # ignore the blank line
awk '1; {print "\n"}' # triple space a line
awk '{print; print "\n"}'
2. delete all blank lines
awk 'NF{print}'
awk NF
awk '/./'
3. precede each line by its line number
awk '{print FNR "\t" $0}'
awk '{print NR "\t" $0}'
awk '{printf("%05d\t%s\n", NR, $0)}'
awk 'NF{$0=++a "\t" $0}; 1' # not print line number if a blank line
4. wc -l
awk '{print NR}'
5. wc -w
awk '{total+=NF} END{print total+0}'
6. sums of the fields of every line
awk '{s=0; for(i=1;i<=NF;i++) s+=$i; print s}'
awk '{for(i=1;i<=NF;i++) s+=$i} END{print s+0}' # undef+0=0
7. print the largest first field and the line that contains it
awk 'max<$1{max=$1;maxline=$0} END{print max maxline}'
awk 'NR==1{max=$1;maxline=$0} max<$1{max=$1;maxline=$0} END{print max maxline}' # it will work if all of first field are negtive number.
8. print the last field of each line
awk '{print $NF}'
9. print every line with more than 3 fields
awk 'NF>3'
10. dos2unix (CRLF -> LF)
tr -d "\015"
tr -d '\r'
awk '{sub(/\r$/,""); print}'
11. unix2dos
awk '{sub(/$/, "\r"); print}'
12. remove trailing space
awk '{sub(/[ \t]+$/, ""); print}'
13. remove heading and trailing space
awk '{gsub(/^[ \t]+|[ \t]+$/, ""); print}'
14. add TAB to heading
awk '{sub{/^/, "\t"); print}'
15. align all text flush right on a 80-column width
awk '{printf("%80s\n", $0)}'
16. center all text on a 80-character width
awk '{len=length; pos=int((80-len)/2; printf("%"(len+pos)"s\n", $0)}'
17. tac
awk '{a(i++)=$0} END{for(j=i-1;j>=0;j--) print a[j]}'
18. if a line ends with a backslash, append the next line to it
$ cat newfile
this is a \
backslash test.
$ awk '/\\$/{sub(/\\$/, ""); getline t; print $0 t; next}; 1'
this is a backslash test.
19. sorted reversely
awk -F':' '{print $1 | "sort -r"}' /etc/passwd
20. delete the second field of every line
awk '{$2=""; print}'
21. print in reverse order the fields of every line
awk '{for(i=NF;i>0;i--) printf("%s ", $i)}; print ""}'
22. uniq
awk 'a!=$0; {a=$0}'
23. sort | uniq
awk '!a[$0]++'
awk '!($0 in a){a[$0]; print}' # efficient
24. contatenate every 5 lines of input with a comma
awk 'ORS=(NR%5 ? "," : "\n"); END{print ""}'
25. head -3
awk 'NR<4'
awk '1; NR==3{exit}' # efficient
awk 'NR>3{exit}; 1'
26. tail -2
awk '{y=x "\n" $0; x=$0} END{print y}' # very inefficient
27. grep
awk '/regex/'
28. grep -v
awk '!/regex/'
29. print the line immediately before a regex
awk '/regex/{print x} {x=$0}'
awk '/regex/{print(x=="" ? "match on line 1" : x)} {x=$0}'
30. print the line immediately after a regex
awk '/regex/{getline; print}'
awk '/regex/{getline x; print(x=="" ? "match on last line" : x)}'
31. grep -E ‘a|b|c’
awk '/a|b|c/'
32. grep ‘a*c’
awk '/a*c/'
33. print only lines of 20 characters or longer
awk 'length>20'
34. sed -n ‘/abc/, $ p’
awk '/abc/, 0' # range pattern
35. print lines 3-8, inclusive
awk 'NR==3, NR==6'
36. print line 3 and line 8
awk 'NR==3; NR==6'
37. print x with 512 times
awk 'BEGIN{while(++a<512) s=s "x"; print s}'
38. merge files
awk 'NR==FNR{a[$0]=1; print} NR>FNR{if(!a[0]) print}'
awk '{a[$0]} END{for(i in a) print a}'
39. sorted by values
awk '{a[j++]=$0} END{len=asort(a); for(i=1;i<=len;i++) print a[i]}'