awk1line

参见: http://www.pement.org

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]}'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值