LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 749|回复: 2

USEFUL ONE-LINE SCRIPTS FOR SED (Unix stream editor) 对sed学习很有帮助

[复制链接]
发表于 2007-8-25 10:41:09 | 显示全部楼层 |阅读模式
都是一行搞定,看完后对sed 会更清楚


  1. -------------------------------------------------------------------------
  2. USEFUL ONE-LINE SCRIPTS FOR SED (Unix stream editor)        Dec. 29, 2005
  3. Compiled by Eric Pement - pemente[at]northpark[dot]edu        version 5.5

  4. Latest version of this file (in English) is usually at:
  5.    http://sed.sourceforge.net/sed1line.txt
  6.    http://www.pement.org/sed/sed1line.txt

  7. This file will also available in other languages:
  8.   Chinese     - http://sed.sourceforge.net/sed1line_zh-CN.html
  9.   Czech       - http://sed.sourceforge.net/sed1line_cz.html
  10.   Dutch       - http://sed.sourceforge.net/sed1line_nl.html
  11.   French      - http://sed.sourceforge.net/sed1line_fr.html
  12.   German      - http://sed.sourceforge.net/sed1line_de.html
  13.   Italian     - (pending)
  14.   Portuguese  - http://sed.sourceforge.net/sed1line_pt-BR.html
  15.   Spanish     - (pending)


  16. FILE SPACING:

  17. # double space a file
  18. sed G

  19. # double space a file which already has blank lines in it. Output file
  20. # should contain no more than one blank line between lines of text.
  21. sed '/^$/d;G'

  22. # triple space a file
  23. sed 'G;G'

  24. # undo double-spacing (assumes even-numbered lines are always blank)
  25. sed 'n;d'

  26. # insert a blank line above every line which matches "regex"
  27. sed '/regex/{x;p;x;}'

  28. # insert a blank line below every line which matches "regex"
  29. sed '/regex/G'

  30. # insert a blank line above and below every line which matches "regex"
  31. sed '/regex/{x;p;x;G;}'

  32. NUMBERING:

  33. # number each line of a file (simple left alignment). Using a tab (see
  34. # note on '\t' at end of file) instead of space will preserve margins.
  35. sed = filename | sed 'N;s/\n/\t/'

  36. # number each line of a file (number on left, right-aligned)
  37. sed = filename | sed 'N; s/^/     /; s/ *\(.\{6,\}\)\n/\1  /'

  38. # number each line of file, but only print numbers if line is not blank
  39. sed '/./=' filename | sed '/./N; s/\n/ /'

  40. # count lines (emulates "wc -l")
  41. sed -n '$='

  42. TEXT CONVERSION AND SUBSTITUTION:

  43. # IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.
  44. sed 's/.$//'               # assumes that all lines end with CR/LF
  45. sed 's/^M$//'              # in bash/tcsh, press Ctrl-V then Ctrl-M
  46. sed 's/\x0D$//'            # works on ssed, gsed 3.02.80 or higher

  47. # IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format.
  48. sed "s/$/`echo -e \\\r`/"            # command line under ksh
  49. sed 's/$'"/`echo \\\r`/"             # command line under bash
  50. sed "s/$/`echo \\\r`/"               # command line under zsh
  51. sed 's/$/\r/'                        # gsed 3.02.80 or higher

  52. # IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format.
  53. sed "s/$//"                          # method 1
  54. sed -n p                             # method 2

  55. # IN DOS ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.
  56. # Can only be done with UnxUtils sed, version 4.0.7 or higher. The
  57. # UnxUtils version can be identified by the custom "--text" switch
  58. # which appears when you use the "--help" switch. Otherwise, changing
  59. # DOS newlines to Unix newlines cannot be done with sed in a DOS
  60. # environment. Use "tr" instead.
  61. sed "s/\r//" infile >outfile         # UnxUtils sed v4.0.7 or higher
  62. tr -d \r <infile >outfile            # GNU tr version 1.22 or higher

  63. # delete leading whitespace (spaces, tabs) from front of each line
  64. # aligns all text flush left
  65. sed 's/^[ \t]*//'                    # see note on '\t' at end of file

  66. # delete trailing whitespace (spaces, tabs) from end of each line
  67. sed 's/[ \t]*$//'                    # see note on '\t' at end of file

  68. # delete BOTH leading and trailing whitespace from each line
  69. sed 's/^[ \t]*//;s/[ \t]*$//'

  70. # insert 5 blank spaces at beginning of each line (make page offset)
  71. sed 's/^/     /'

  72. # align all text flush right on a 79-column width
  73. sed -e :a -e 's/^.\{1,78\}$/ &/;ta'  # set at 78 plus 1 space

  74. # center all text in the middle of 79-column width. In method 1,
  75. # spaces at the beginning of the line are significant, and trailing
  76. # spaces are appended at the end of the line. In method 2, spaces at
  77. # the beginning of the line are discarded in centering the line, and
  78. # no trailing spaces appear at the end of lines.
  79. sed  -e :a -e 's/^.\{1,77\}$/ & /;ta'                     # method 1
  80. sed  -e :a -e 's/^.\{1,77\}$/ &/;ta' -e 's/\( *\)\1/\1/'  # method 2

  81. # substitute (find and replace) "foo" with "bar" on each line
  82. sed 's/foo/bar/'             # replaces only 1st instance in a line
  83. sed 's/foo/bar/4'            # replaces only 4th instance in a line
  84. sed 's/foo/bar/g'            # replaces ALL instances in a line
  85. sed 's/\(.*\)foo\(.*foo\)/\1bar\2/' # replace the next-to-last case
  86. sed 's/\(.*\)foo/\1bar/'            # replace only the last case

  87. # substitute "foo" with "bar" ONLY for lines which contain "baz"
  88. sed '/baz/s/foo/bar/g'

  89. # substitute "foo" with "bar" EXCEPT for lines which contain "baz"
  90. sed '/baz/!s/foo/bar/g'

  91. # change "scarlet" or "ruby" or "puce" to "red"
  92. sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g'   # most seds
  93. gsed 's/scarlet\|ruby\|puce/red/g'                # GNU sed only

  94. # reverse order of lines (emulates "tac")
  95. # bug/feature in HHsed v1.5 causes blank lines to be deleted
  96. sed '1!G;h;$!d'               # method 1
  97. sed -n '1!G;h;$p'             # method 2

  98. # reverse each character on the line (emulates "rev")
  99. sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'

  100. # join pairs of lines side-by-side (like "paste")
  101. sed '$!N;s/\n/ /'

  102. # if a line ends with a backslash, append the next line to it
  103. sed -e :a -e '/\\$/N; s/\\\n//; ta'

  104. # if a line begins with an equal sign, append it to the previous line
  105. # and replace the "=" with a single space
  106. sed -e :a -e '$!N;s/\n=/ /;ta' -e 'P;D'

  107. # add commas to numeric strings, changing "1234567" to "1,234,567"
  108. gsed ':a;s/\B[0-9]\{3\}\>/,&/;ta'                     # GNU sed
  109. sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'  # other seds

  110. # add commas to numbers with decimal points and minus signs (GNU sed)
  111. gsed -r ':a;s/(^|[^0-9.])([0-9]+)([0-9]{3})/\1\2,\3/g;ta'

  112. # add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)
  113. gsed '0~5G'                  # GNU sed only
  114. sed 'n;n;n;n;G;'             # other seds

  115. SELECTIVE PRINTING OF CERTAIN LINES:

  116. # print first 10 lines of file (emulates behavior of "head")
  117. sed 10q

  118. # print first line of file (emulates "head -1")
  119. sed q

  120. # print the last 10 lines of a file (emulates "tail")
  121. sed -e :a -e '$q;N;11,$D;ba'

  122. # print the last 2 lines of a file (emulates "tail -2")
  123. sed '$!N;$!D'

  124. # print the last line of a file (emulates "tail -1")
  125. sed '$!d'                    # method 1
  126. sed -n '$p'                  # method 2

  127. # print the next-to-the-last line of a file
  128. sed -e '$!{h;d;}' -e x              # for 1-line files, print blank line
  129. sed -e '1{$q;}' -e '$!{h;d;}' -e x  # for 1-line files, print the line
  130. sed -e '1{$d;}' -e '$!{h;d;}' -e x  # for 1-line files, print nothing

  131. # print only lines which match regular expression (emulates "grep")
  132. sed -n '/regexp/p'           # method 1
  133. sed '/regexp/!d'             # method 2

  134. # print only lines which do NOT match regexp (emulates "grep -v")
  135. sed -n '/regexp/!p'          # method 1, corresponds to above
  136. sed '/regexp/d'              # method 2, simpler syntax

  137. # print the line immediately before a regexp, but not the line
  138. # containing the regexp
  139. sed -n '/regexp/{g;1!p;};h'

  140. # print the line immediately after a regexp, but not the line
  141. # containing the regexp
  142. sed -n '/regexp/{n;p;}'

  143. # print 1 line of context before and after regexp, with line number
  144. # indicating where the regexp occurred (similar to "grep -A1 -B1")
  145. sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h

  146. # grep for AAA and BBB and CCC (in any order)
  147. sed '/AAA/!d; /BBB/!d; /CCC/!d'

  148. # grep for AAA and BBB and CCC (in that order)
  149. sed '/AAA.*BBB.*CCC/!d'

  150. # grep for AAA or BBB or CCC (emulates "egrep")
  151. sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d    # most seds
  152. gsed '/AAA\|BBB\|CCC/!d'                        # GNU sed only

  153. # print paragraph if it contains AAA (blank lines separate paragraphs)
  154. # HHsed v1.5 must insert a 'G;' after 'x;' in the next 3 scripts below
  155. sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'

  156. # print paragraph if it contains AAA and BBB and CCC (in any order)
  157. sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'

  158. # print paragraph if it contains AAA or BBB or CCC
  159. sed -e '/./{H;$!d;}' -e 'x;/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d
  160. gsed '/./{H;$!d;};x;/AAA\|BBB\|CCC/b;d'         # GNU sed only

  161. # print only lines of 65 characters or longer
  162. sed -n '/^.\{65\}/p'

  163. # print only lines of less than 65 characters
  164. sed -n '/^.\{65\}/!p'        # method 1, corresponds to above
  165. sed '/^.\{65\}/d'            # method 2, simpler syntax

  166. # print section of file from regular expression to end of file
  167. sed -n '/regexp/,$p'

  168. # print section of file based on line numbers (lines 8-12, inclusive)
  169. sed -n '8,12p'               # method 1
  170. sed '8,12!d'                 # method 2

  171. # print line number 52
  172. sed -n '52p'                 # method 1
  173. sed '52!d'                   # method 2
  174. sed '52q;d'                  # method 3, efficient on large files

  175. # beginning at line 3, print every 7th line
  176. gsed -n '3~7p'               # GNU sed only
  177. sed -n '3,${p;n;n;n;n;n;n;}' # other seds

  178. # print section of file between two regular expressions (inclusive)
  179. sed -n '/Iowa/,/Montana/p'             # case sensitive

  180. SELECTIVE DELETION OF CERTAIN LINES:

  181. # print all of file EXCEPT section between 2 regular expressions
  182. sed '/Iowa/,/Montana/d'

  183. # delete duplicate, consecutive lines from a file (emulates "uniq").
  184. # First line in a set of duplicate lines is kept, rest are deleted.
  185. sed '$!N; /^\(.*\)\n\1$/!P; D'

  186. # delete duplicate, nonconsecutive lines from a file. Beware not to
  187. # overflow the buffer size of the hold space, or else use GNU sed.
  188. sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'

  189. # delete all lines except duplicate lines (emulates "uniq -d").
  190. sed '$!N; s/^\(.*\)\n\1$/\1/; t; D'

  191. # delete the first 10 lines of a file
  192. sed '1,10d'

  193. # delete the last line of a file
  194. sed '$d'

  195. # delete the last 2 lines of a file
  196. sed 'N;$!P;$!D;$d'

  197. # delete the last 10 lines of a file
  198. sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1
  199. sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2

  200. # delete every 8th line
  201. gsed '0~8d'                           # GNU sed only
  202. sed 'n;n;n;n;n;n;n;d;'                # other seds

  203. # delete lines matching pattern
  204. sed '/pattern/d'

  205. # delete ALL blank lines from a file (same as "grep '.' ")
  206. sed '/^$/d'                           # method 1
  207. sed '/./!d'                           # method 2

  208. # delete all CONSECUTIVE blank lines from file except the first; also
  209. # deletes all blank lines from top and end of file (emulates "cat -s")
  210. sed '/./,/^$/!d'          # method 1, allows 0 blanks at top, 1 at EOF
  211. sed '/^$/N;/\n$/D'        # method 2, allows 1 blank at top, 0 at EOF

  212. # delete all CONSECUTIVE blank lines from file except the first 2:
  213. sed '/^$/N;/\n$/N;//D'

  214. # delete all leading blank lines at top of file
  215. sed '/./,$!d'

  216. # delete all trailing blank lines at end of file
  217. sed -e :a -e '/^\n*$/{$d;N;ba' -e '}'  # works on all seds
  218. sed -e :a -e '/^\n*$/N;/\n$/ba'        # ditto, except for gsed 3.02.*

  219. # delete the last line of each paragraph
  220. sed -n '/^$/{p;h;};/./{x;/./p;}'

  221. SPECIAL APPLICATIONS:

  222. # remove nroff overstrikes (char, backspace) from man pages. The 'echo'
  223. # command may need an -e switch if you use Unix System V or bash shell.
  224. sed "s/.`echo \\\b`//g"    # double quotes required for Unix environment
  225. sed 's/.^H//g'             # in bash/tcsh, press Ctrl-V and then Ctrl-H
  226. sed 's/.\x08//g'           # hex expression for sed 1.5, GNU sed, ssed

  227. # get Usenet/e-mail message header
  228. sed '/^$/q'                # deletes everything after first blank line

  229. # get Usenet/e-mail message body
  230. sed '1,/^$/d'              # deletes everything up to first blank line

  231. # get Subject header, but remove initial "Subject: " portion
  232. sed '/^Subject: */!d; s///;q'

  233. # get return address header
  234. sed '/^Reply-To:/q; /^From:/h; /./d;g;q'

  235. # parse out the address proper. Pulls out the e-mail address by itself
  236. # from the 1-line return address header (see preceding script)
  237. sed 's/ *(.*)//; s/>.*//; s/.*[:<] *//'

  238. # add a leading angle bracket and space to each line (quote a message)
  239. sed 's/^/> /'

  240. # delete leading angle bracket & space from each line (unquote a message)
  241. sed 's/^> //'

  242. # remove most HTML tags (accommodates multiple-line tags)
  243. sed -e :a -e 's/<[^>]*>//g;/</N;//ba'

  244. # extract multi-part uuencoded binaries, removing extraneous header
  245. # info, so that only the uuencoded portion remains. Files passed to
  246. # sed must be passed in the proper order. Version 1 can be entered
  247. # from the command line; version 2 can be made into an executable
  248. # Unix shell script. (Modified from a script by Rahul Dhesi.)
  249. sed '/^end/,/^begin/d' file1 file2 ... fileX | uudecode   # vers. 1
  250. sed '/^end/,/^begin/d' "$@" | uudecode                    # vers. 2

  251. # sort paragraphs of file alphabetically. Paragraphs are separated by blank
  252. # lines. GNU sed uses \v for vertical tab, or any unique char will do.
  253. sed '/./{H;d;};x;s/\n/={NL}=/g' file | sort | sed '1s/={NL}=//;s/={NL}=/\n/g'
  254. gsed '/./{H;d};x;y/\n/\v/' file | sort | sed '1s/\v//;y/\v/\n/'

  255. # zip up each .TXT file individually, deleting the source file and
  256. # setting the name of each .ZIP file to the basename of the .TXT file
  257. # (under DOS: the "dir /b" switch returns bare filenames in all caps).
  258. echo @echo off >zipup.bat
  259. dir /b *.txt | sed "s/^\(.*\)\.TXT/pkzip -mo \1 \1.TXT/" >>zipup.bat

  260. TYPICAL USE: Sed takes one or more editing commands and applies all of
  261. them, in sequence, to each line of input. After all the commands have
  262. been applied to the first input line, that line is output and a second
  263. input line is taken for processing, and the cycle repeats. The
  264. preceding examples assume that input comes from the standard input
  265. device (i.e, the console, normally this will be piped input). One or
  266. more filenames can be appended to the command line if the input does
  267. not come from stdin. Output is sent to stdout (the screen). Thus:

  268. cat filename | sed '10q'        # uses piped input
  269. sed '10q' filename              # same effect, avoids a useless "cat"
  270. sed '10q' filename > newfile    # redirects output to disk

  271. For additional syntax instructions, including the way to apply editing
  272. commands from a disk file instead of the command line, consult "sed &
  273. awk, 2nd Edition," by Dale Dougherty and Arnold Robbins (O'Reilly,
  274. 1997; http://www.ora.com), "UNIX Text Processing," by Dale Dougherty
  275. and Tim O'Reilly (Hayden Books, 1987) or the tutorials by Mike Arst
  276. distributed in U-SEDIT2.ZIP (many sites). To fully exploit the power
  277. of sed, one must understand "regular expressions." For this, see
  278. "Mastering Regular Expressions" by Jeffrey Friedl (O'Reilly, 1997).
  279. The manual ("man") pages on Unix systems may be helpful (try "man
  280. sed", "man regexp", or the subsection on regular expressions in "man
  281. ed"), but man pages are notoriously difficult. They are not written to
  282. teach sed use or regexps to first-time users, but as a reference text
  283. for those already acquainted with these tools.

  284. QUOTING SYNTAX: The preceding examples use single quotes ('...')
  285. instead of double quotes ("...") to enclose editing commands, since
  286. sed is typically used on a Unix platform. Single quotes prevent the
  287. Unix shell from intrepreting the dollar sign ($) and backquotes
  288. (`...`), which are expanded by the shell if they are enclosed in
  289. double quotes. Users of the "csh" shell and derivatives will also need
  290. to quote the exclamation mark (!) with the backslash (i.e., \!) to
  291. properly run the examples listed above, even within single quotes.
  292. Versions of sed written for DOS invariably require double quotes
  293. ("...") instead of single quotes to enclose editing commands.

  294. USE OF '\t' IN SED SCRIPTS: For clarity in documentation, we have used
  295. the expression '\t' to indicate a tab character (0x09) in the scripts.
  296. However, most versions of sed do not recognize the '\t' abbreviation,
  297. so when typing these scripts from the command line, you should press
  298. the TAB key instead. '\t' is supported as a regular expression
  299. metacharacter in awk, perl, and HHsed, sedmod, and GNU sed v3.02.80.

  300. VERSIONS OF SED: Versions of sed do differ, and some slight syntax
  301. variation is to be expected. In particular, most do not support the
  302. use of labels (:name) or branch instructions (b,t) within editing
  303. commands, except at the end of those commands. We have used the syntax
  304. which will be portable to most users of sed, even though the popular
  305. GNU versions of sed allow a more succinct syntax. When the reader sees
  306. a fairly long command such as this:

  307.    sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d

  308. it is heartening to know that GNU sed will let you reduce it to:

  309.    sed '/AAA/b;/BBB/b;/CCC/b;d'      # or even
  310.    sed '/AAA\|BBB\|CCC/b;d'

  311. In addition, remember that while many versions of sed accept a command
  312. like "/one/ s/RE1/RE2/", some do NOT allow "/one/! s/RE1/RE2/", which
  313. contains space before the 's'. Omit the space when typing the command.

  314. OPTIMIZING FOR SPEED: If execution speed needs to be increased (due to
  315. large input files or slow processors or hard disks), substitution will
  316. be executed more quickly if the "find" expression is specified before
  317. giving the "s/.../.../" instruction. Thus:

  318.    sed 's/foo/bar/g' filename         # standard replace command
  319.    sed '/foo/ s/foo/bar/g' filename   # executes more quickly
  320.    sed '/foo/ s//bar/g' filename      # shorthand sed syntax

  321. On line selection or deletion in which you only need to output lines
  322. from the first part of the file, a "quit" command (q) in the script
  323. will drastically reduce processing time for large files. Thus:

  324.    sed -n '45,50p' filename           # print line nos. 45-50 of a file
  325.    sed -n '51q;45,50p' filename       # same, but executes much faster

  326. If you have any additional scripts to contribute or if you find errors
  327. in this document, please send e-mail to the compiler. Indicate the
  328. version of sed you used, the operating system it was compiled for, and
  329. the nature of the problem. To qualify as a one-liner, the command line
  330. must be 65 characters or less. Various scripts in this file have been
  331. written or contributed by:

  332. Al Aab                   # founder of "seders" list
  333. Edgar Allen              # various
  334. Yiorgos Adamopoulos      # various
  335. Dale Dougherty           # author of "sed & awk"
  336. Carlos Duarte            # author of "do it with sed"
  337. Eric Pement              # author of this document
  338. Ken Pizzini              # author of GNU sed v3.02
  339. S.G. Ravenhall           # great de-html script
  340. Greg Ubben               # many contributions & much help
  341. -------------------------------------------------------------------------


复制代码
发表于 2007-8-25 23:16:55 | 显示全部楼层
Good Job!
回复 支持 反对

使用道具 举报

发表于 2007-8-27 21:16:29 | 显示全部楼层
好东西

顶一下
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表