免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
论坛 程序设计 Shell AWK1LINE
最近访问板块 发新帖
查看: 2458 | 回复: 6
打印 上一主题 下一主题

AWK1LINE [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-01-07 11:27 |只看该作者 |倒序浏览
前面已经研究了 SED1LINE. 下面贴出 AWK1LINE 作个对照, 看看
sed 与 awk 如何处理同一个问题。



  1. HANDY ONE-LINERS FOR AWK                                  22 July 2003
  2. compiled by Eric Pement <pemente@northpark.edu>           version 0.22
  3.    

  4. USAGE:

  5.     Unix:  awk '/pattern/ {print "$1"}'    # standard Unix shells
  6. DOS/Win:  awk '/pattern/ {print "$1"}'    # okay for DJGPP compiled
  7.            awk "/pattern/ {print \"$1\"}"  # required for Mingw32

  8. Most of my experience comes from version of GNU awk (gawk) compiled for
  9. Win32. Note in particular that DJGPP compilations permit the awk script
  10. to follow Unix quoting syntax '/like/ {"this"}'. However, the user must
  11. know that single quotes under DOS/Windows do not protect the redirection
  12. arrows (<, >) nor do they protect pipes (|). Both are special symbols
  13. for the DOS/CMD command shell and their special meaning is ignored only
  14. if they are placed within "double quotes." Likewise, DOS/Win users must
  15. remember that the percent sign (%) is used to mark DOS/Win environment
  16. variables, so it must be doubled (%%) to yield a single percent sign
  17. visible to awk.

  18. If I am sure that a script will NOT need to be quoted in Unix, DOS, or
  19. CMD, then I normally omit the quote marks. If an example is peculiar to
  20. GNU awk, the command 'gawk' will be used. Please notify me if you find
  21. errors or new commands to add to this list (total length under 65
  22. characters). I usually try to put the shortest script first.

  23. FILE SPACING:

  24. # double space a file
  25. awk '1;{print ""}'
  26. awk 'BEGIN{ORS="\n\n"};1'

  27. # double space a file which already has blank lines in it. Output file
  28. # should contain no more than one blank line between lines of text.
  29. # NOTE: On Unix systems, DOS lines which have only CRLF (\r\n) are
  30. # often treated as non-blank, and thus 'NF' alone will return TRUE.
  31. awk 'NF{print $0 "\n"}'

  32. # triple space a file
  33. awk '1;{print "\n"}'

  34. NUMBERING AND CALCULATIONS:

  35. # precede each line by its line number FOR THAT FILE (left alignment).
  36. # Using a tab (\t) instead of space will preserve margins.
  37. awk '{print FNR "\t" $0}' files*

  38. # precede each line by its line number FOR ALL FILES TOGETHER, with tab.
  39. awk '{print NR "\t" $0}' files*

  40. # number each line of a file (number on left, right-aligned)
  41. # Double the percent signs if typing from the DOS command prompt.
  42. awk '{printf("%5d : %s\n", NR,$0)}'

  43. # number each line of file, but only print numbers if line is not blank
  44. # Remember caveats about Unix treatment of \r (mentioned above)
  45. awk 'NF{$0=++a " :" $0};{print}'
  46. awk '{print (NF? ++a " :" :"") $0}'

  47. # count lines (emulates "wc -l")
  48. awk 'END{print NR}'

  49. # print the sums of the fields of every line
  50. awk '{s=0; for (i=1; i<=NF; i++) s=s+$i; print s}'

  51. # add all fields in all lines and print the sum
  52. awk '{for (i=1; i<=NF; i++) s=s+$i}; END{print s}'

  53. # print every line after replacing each field with its absolute value
  54. awk '{for (i=1; i<=NF; i++) if ($i < 0) $i = -$i; print }'
  55. awk '{for (i=1; i<=NF; i++) $i = ($i < 0) ? -$i : $i; print }'

  56. # print the total number of fields ("words") in all lines
  57. awk '{ total = total + NF }; END {print total}' file

  58. # print the total number of lines that contain "Beth"
  59. awk '/Beth/{n++}; END {print n+0}' file

  60. # print the largest first field and the line that contains it
  61. # Intended for finding the longest string in field #1
  62. awk '$1 > max {max=$1; maxline=$0}; END{ print max, maxline}'

  63. # print the number of fields in each line, followed by the line
  64. awk '{ print NF ":" $0 } '

  65. # print the last field of each line
  66. awk '{ print $NF }'

  67. # print the last field of the last line
  68. awk '{ field = $NF }; END{ print field }'

  69. # print every line with more than 4 fields
  70. awk 'NF > 4'

  71. # print every line where the value of the last field is > 4
  72. awk '$NF > 4'


  73. TEXT CONVERSION AND SUBSTITUTION:

  74. # IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format
  75. awk '{sub(/\r$/,"");print}'   # assumes EACH line ends with Ctrl-M

  76. # IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format
  77. awk '{sub(/$/,"\r");print}

  78. # IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format
  79. awk 1

  80. # IN DOS ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format
  81. # Cannot be done with DOS versions of awk, other than gawk:
  82. gawk -v BINMODE="w" '1' infile >outfile

  83. # Use "tr" instead.
  84. tr -d \r <infile >outfile            # GNU tr version 1.22 or higher

  85. # delete leading whitespace (spaces, tabs) from front of each line
  86. # aligns all text flush left
  87. awk '{sub(/^[ \t]+/, ""); print}'

  88. # delete trailing whitespace (spaces, tabs) from end of each line
  89. awk '{sub(/[ \t]+$/, "");print}'

  90. # delete BOTH leading and trailing whitespace from each line
  91. awk '{gsub(/^[ \t]+|[ \t]+$/,"");print}'
  92. awk '{$1=$1;print}'           # also removes extra space between fields

  93. # insert 5 blank spaces at beginning of each line (make page offset)
  94. awk '{sub(/^/, "     ");print}'

  95. # align all text flush right on a 79-column width
  96. awk '{printf "%79s\n", $0}' file*

  97. # center all text on a 79-character width
  98. awk '{l=length();s=int((79-l)/2); printf "%"(s+l)"s\n",$0}' file*

  99. # substitute (find and replace) "foo" with "bar" on each line
  100. awk '{sub(/foo/,"bar");print}'           # replaces only 1st instance
  101. gawk '{$0=gensub(/foo/,"bar",4);print}'  # replaces only 4th instance
  102. awk '{gsub(/foo/,"bar");print}'          # replaces ALL instances in a line

  103. # substitute "foo" with "bar" ONLY for lines which contain "baz"
  104. awk '/baz/{gsub(/foo/, "bar")};{print}'

  105. # substitute "foo" with "bar" EXCEPT for lines which contain "baz"
  106. awk '!/baz/{gsub(/foo/, "bar")};{print}'

  107. # change "scarlet" or "ruby" or "puce" to "red"
  108. awk '{gsub(/scarlet|ruby|puce/, "red"); print}'

  109. # reverse order of lines (emulates "tac")
  110. awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' file*

  111. # if a line ends with a backslash, append the next line to it
  112. # (fails if there are multiple lines ending with backslash...)
  113. awk '/\\$/ {sub(/\\$/,""); getline t; print $0 t; next}; 1' file*

  114. # print and sort the login names of all users
  115. awk -F ":" '{ print $1 | "sort" }' /etc/passwd

  116. # print the first 2 fields, in opposite order, of every line
  117. awk '{print $2, $1}' file

  118. # switch the first 2 fields of every line
  119. awk '{temp = $1; $1 = $2; $2 = temp}' file

  120. # print every line, deleting the second field of that line
  121. awk '{ $2 = ""; print }'

  122. # print in reverse order the fields of every line
  123. awk '{for (i=NF; i>0; i--) printf("%s ",i);printf ("\n")}' file

  124. # remove duplicate, consecutive lines (emulates "uniq")
  125. awk 'a !~ $0; {a=$0}'

  126. # remove duplicate, nonconsecutive lines
  127. awk '! a[$0]++'                     # most concise script
  128. awk '!($0 in a) {a[$0];print}'      # most efficient script

  129. # concatenate every 5 lines of input, using a comma separator
  130. # between fields
  131. awk 'ORS=%NR%5?",":"\n"' file



  132. SELECTIVE PRINTING OF CERTAIN LINES:

  133. # print first 10 lines of file (emulates behavior of "head")
  134. awk 'NR < 11'

  135. # print first line of file (emulates "head -1")
  136. awk 'NR>1{exit};1'

  137.   # print the last 2 lines of a file (emulates "tail -2")
  138. awk '{y=x "\n" $0; x=$0};END{print y}'

  139. # print the last line of a file (emulates "tail -1")
  140. awk 'END{print}'

  141. # print only lines which match regular expression (emulates "grep")
  142. awk '/regex/'

  143. # print only lines which do NOT match regex (emulates "grep -v")
  144. awk '!/regex/'

  145. # print the line immediately before a regex, but not the line
  146. # containing the regex
  147. awk '/regex/{print x};{x=$0}'
  148. awk '/regex/{print (x=="" ? "match on line 1" : x)};{x=$0}'

  149. # print the line immediately after a regex, but not the line
  150. # containing the regex
  151. awk '/regex/{getline;print}'

  152. # grep for AAA and BBB and CCC (in any order)
  153. awk '/AAA/; /BBB/; /CCC/'

  154. # grep for AAA and BBB and CCC (in that order)
  155. awk '/AAA.*BBB.*CCC/'

  156. # print only lines of 65 characters or longer
  157. awk 'length > 64'

  158. # print only lines of less than 65 characters
  159. awk 'length < 64'

  160. # print section of file from regular expression to end of file
  161. awk '/regex/,0'
  162. awk '/regex/,EOF'

  163. # print section of file based on line numbers (lines 8-12, inclusive)
  164. awk 'NR==8,NR==12'

  165. # print line number 52
  166. awk 'NR==52'
  167. awk 'NR==52 {print;exit}'          # more efficient on large files

  168. # print section of file between two regular expressions (inclusive)
  169. awk '/Iowa/,/Montana/'             # case sensitive


  170. SELECTIVE DELETION OF CERTAIN LINES:

  171. # delete ALL blank lines from a file (same as "grep '.' ")
  172. awk NF
  173. awk '/./'


  174. CREDITS AND THANKS:

  175. Special thanks to Peter S. Tillier for helping me with the first release
  176. of this FAQ file.

  177. For additional syntax instructions, including the way to apply editing
  178. commands from a disk file instead of the command line, consult:

  179. "sed & awk, 2nd Edition," by Dale Dougherty and Arnold Robbins
  180.   O'Reilly, 1997
  181. "UNIX Text Processing," by Dale Dougherty and Tim O'Reilly
  182.   Hayden Books, 1987
  183. "Effective awk Programming, 3rd Edition." by Arnold Robbins
  184.   O'Reilly, 2001

  185. To fully exploit the power of awk, one must understand "regular
  186. expressions." For detailed discussion of regular expressions, see
  187. "Mastering Regular Expressions, 2d edition" by Jeffrey Friedl
  188.    (O'Reilly, 2002).

  189. The manual ("man") pages on Unix systems may be helpful (try "man awk",
  190. "man nawk", "man regexp", or the section on regular expressions in "man
  191. ed"), but man pages are notoriously difficult. They are not written to
  192. teach awk use or regexps to first-time users, but as a reference text
  193. for those already acquainted with these tools.

  194. USE OF '\t' IN awk SCRIPTS: For clarity in documentation, we have used
  195. the expression '\t' to indicate a tab character (0x09) in the scripts.
  196. All versions of awk, even the UNIX System 7 version should recognize
  197. the '\t' abbreviation.

  198. #---end of file---

复制代码

论坛徽章:
0
2 [报告]
发表于 2005-01-07 11:44 |只看该作者

AWK1LINE

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
3 [报告]
发表于 2005-01-07 12:13 |只看该作者

AWK1LINE

Eric Pement就是牛叉

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
4 [报告]
发表于 2005-01-07 20:30 |只看该作者

AWK1LINE

好文~~~~,哪位兄弟给翻译一下,就更棒啦!
先收藏!!!

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
5 [报告]
发表于 2005-01-07 21:07 |只看该作者

AWK1LINE

呵,支持!

论坛徽章:
0
6 [报告]
发表于 2005-01-07 21:16 |只看该作者

AWK1LINE

收藏了,不单有助于学awk,同时还鼓励大家学习一下英文。

论坛徽章:
0
7 [报告]
发表于 2005-01-08 02:00 |只看该作者

AWK1LINE

真是棒!
有些用法都没见过!

BTW:那日,看SED1LINE,狂赞其精妙,称其实现了wc、tail等等cmd功能,一同事说“那他不干嘛用sed写个sed”!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP