MaintenanceGuidelinesTips: sed1line.txt

File sed1line.txt, 18.4 KB (added by Fran Boon, 11 years ago)
Line 
1-------------------------------------------------------------------------
2USEFUL ONE-LINE SCRIPTS FOR SED (Unix stream editor) Dec. 29, 2005
3Compiled by Eric Pement - pemente[at]northpark[dot]edu version 5.5
4
5Latest version of this file (in English) is usually at:
6 http://sed.sourceforge.net/sed1line.txt
7 http://www.pement.org/sed/sed1line.txt
8
9This file will also available in other languages:
10 Chinese - http://sed.sourceforge.net/sed1line_zh-CN.html
11 Czech - http://sed.sourceforge.net/sed1line_cz.html
12 Dutch - http://sed.sourceforge.net/sed1line_nl.html
13 French - http://sed.sourceforge.net/sed1line_fr.html
14 German - http://sed.sourceforge.net/sed1line_de.html
15 Italian - (pending)
16 Portuguese - http://sed.sourceforge.net/sed1line_pt-BR.html
17 Spanish - (pending)
18
19
20FILE SPACING:
21
22 # double space a file
23 sed G
24
25 # double space a file which already has blank lines in it. Output file
26 # should contain no more than one blank line between lines of text.
27 sed '/^$/d;G'
28
29 # triple space a file
30 sed 'G;G'
31
32 # undo double-spacing (assumes even-numbered lines are always blank)
33 sed 'n;d'
34
35 # insert a blank line above every line which matches "regex"
36 sed '/regex/{x;p;x;}'
37
38 # insert a blank line below every line which matches "regex"
39 sed '/regex/G'
40
41 # insert a blank line above and below every line which matches "regex"
42 sed '/regex/{x;p;x;G;}'
43
44NUMBERING:
45
46 # number each line of a file (simple left alignment). Using a tab (see
47 # note on '\t' at end of file) instead of space will preserve margins.
48 sed = filename | sed 'N;s/\n/\t/'
49
50 # number each line of a file (number on left, right-aligned)
51 sed = filename | sed 'N; s/^/ /; s/ *\(.\{6,\}\)\n/\1 /'
52
53 # number each line of file, but only print numbers if line is not blank
54 sed '/./=' filename | sed '/./N; s/\n/ /'
55
56 # count lines (emulates "wc -l")
57 sed -n '$='
58
59TEXT CONVERSION AND SUBSTITUTION:
60
61 # IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.
62 sed 's/.$//' # assumes that all lines end with CR/LF
63 sed 's/^M$//' # in bash/tcsh, press Ctrl-V then Ctrl-M
64 sed 's/\x0D$//' # works on ssed, gsed 3.02.80 or higher
65
66 # IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format.
67 sed "s/$/`echo -e \\\r`/" # command line under ksh
68 sed 's/$'"/`echo \\\r`/" # command line under bash
69 sed "s/$/`echo \\\r`/" # command line under zsh
70 sed 's/$/\r/' # gsed 3.02.80 or higher
71
72 # IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format.
73 sed "s/$//" # method 1
74 sed -n p # method 2
75
76 # IN DOS ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.
77 # Can only be done with UnxUtils sed, version 4.0.7 or higher. The
78 # UnxUtils version can be identified by the custom "--text" switch
79 # which appears when you use the "--help" switch. Otherwise, changing
80 # DOS newlines to Unix newlines cannot be done with sed in a DOS
81 # environment. Use "tr" instead.
82 sed "s/\r//" infile >outfile # UnxUtils sed v4.0.7 or higher
83 tr -d \r <infile >outfile # GNU tr version 1.22 or higher
84
85 # delete leading whitespace (spaces, tabs) from front of each line
86 # aligns all text flush left
87 sed 's/^[ \t]*//' # see note on '\t' at end of file
88
89 # delete trailing whitespace (spaces, tabs) from end of each line
90 sed 's/[ \t]*$//' # see note on '\t' at end of file
91
92 # delete BOTH leading and trailing whitespace from each line
93 sed 's/^[ \t]*//;s/[ \t]*$//'
94
95 # insert 5 blank spaces at beginning of each line (make page offset)
96 sed 's/^/ /'
97
98 # align all text flush right on a 79-column width
99 sed -e :a -e 's/^.\{1,78\}$/ &/;ta' # set at 78 plus 1 space
100
101 # center all text in the middle of 79-column width. In method 1,
102 # spaces at the beginning of the line are significant, and trailing
103 # spaces are appended at the end of the line. In method 2, spaces at
104 # the beginning of the line are discarded in centering the line, and
105 # no trailing spaces appear at the end of lines.
106 sed -e :a -e 's/^.\{1,77\}$/ & /;ta' # method 1
107 sed -e :a -e 's/^.\{1,77\}$/ &/;ta' -e 's/\( *\)\1/\1/' # method 2
108
109 # substitute (find and replace) "foo" with "bar" on each line
110 sed 's/foo/bar/' # replaces only 1st instance in a line
111 sed 's/foo/bar/4' # replaces only 4th instance in a line
112 sed 's/foo/bar/g' # replaces ALL instances in a line
113 sed 's/\(.*\)foo\(.*foo\)/\1bar\2/' # replace the next-to-last case
114 sed 's/\(.*\)foo/\1bar/' # replace only the last case
115
116 # substitute "foo" with "bar" ONLY for lines which contain "baz"
117 sed '/baz/s/foo/bar/g'
118
119 # substitute "foo" with "bar" EXCEPT for lines which contain "baz"
120 sed '/baz/!s/foo/bar/g'
121
122 # change "scarlet" or "ruby" or "puce" to "red"
123 sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g' # most seds
124 gsed 's/scarlet\|ruby\|puce/red/g' # GNU sed only
125
126 # reverse order of lines (emulates "tac")
127 # bug/feature in HHsed v1.5 causes blank lines to be deleted
128 sed '1!G;h;$!d' # method 1
129 sed -n '1!G;h;$p' # method 2
130
131 # reverse each character on the line (emulates "rev")
132 sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
133
134 # join pairs of lines side-by-side (like "paste")
135 sed '$!N;s/\n/ /'
136
137 # if a line ends with a backslash, append the next line to it
138 sed -e :a -e '/\\$/N; s/\\\n//; ta'
139
140 # if a line begins with an equal sign, append it to the previous line
141 # and replace the "=" with a single space
142 sed -e :a -e '$!N;s/\n=/ /;ta' -e 'P;D'
143
144 # add commas to numeric strings, changing "1234567" to "1,234,567"
145 gsed ':a;s/\B[0-9]\{3\}\>/,&/;ta' # GNU sed
146 sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' # other seds
147
148 # add commas to numbers with decimal points and minus signs (GNU sed)
149 gsed -r ':a;s/(^|[^0-9.])([0-9]+)([0-9]{3})/\1\2,\3/g;ta'
150
151 # add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)
152 gsed '0~5G' # GNU sed only
153 sed 'n;n;n;n;G;' # other seds
154
155SELECTIVE PRINTING OF CERTAIN LINES:
156
157 # print first 10 lines of file (emulates behavior of "head")
158 sed 10q
159
160 # print first line of file (emulates "head -1")
161 sed q
162
163 # print the last 10 lines of a file (emulates "tail")
164 sed -e :a -e '$q;N;11,$D;ba'
165
166 # print the last 2 lines of a file (emulates "tail -2")
167 sed '$!N;$!D'
168
169 # print the last line of a file (emulates "tail -1")
170 sed '$!d' # method 1
171 sed -n '$p' # method 2
172
173 # print the next-to-the-last line of a file
174 sed -e '$!{h;d;}' -e x # for 1-line files, print blank line
175 sed -e '1{$q;}' -e '$!{h;d;}' -e x # for 1-line files, print the line
176 sed -e '1{$d;}' -e '$!{h;d;}' -e x # for 1-line files, print nothing
177
178 # print only lines which match regular expression (emulates "grep")
179 sed -n '/regexp/p' # method 1
180 sed '/regexp/!d' # method 2
181
182 # print only lines which do NOT match regexp (emulates "grep -v")
183 sed -n '/regexp/!p' # method 1, corresponds to above
184 sed '/regexp/d' # method 2, simpler syntax
185
186 # print the line immediately before a regexp, but not the line
187 # containing the regexp
188 sed -n '/regexp/{g;1!p;};h'
189
190 # print the line immediately after a regexp, but not the line
191 # containing the regexp
192 sed -n '/regexp/{n;p;}'
193
194 # print 1 line of context before and after regexp, with line number
195 # indicating where the regexp occurred (similar to "grep -A1 -B1")
196 sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h
197
198 # grep for AAA and BBB and CCC (in any order)
199 sed '/AAA/!d; /BBB/!d; /CCC/!d'
200
201 # grep for AAA and BBB and CCC (in that order)
202 sed '/AAA.*BBB.*CCC/!d'
203
204 # grep for AAA or BBB or CCC (emulates "egrep")
205 sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d # most seds
206 gsed '/AAA\|BBB\|CCC/!d' # GNU sed only
207
208 # print paragraph if it contains AAA (blank lines separate paragraphs)
209 # HHsed v1.5 must insert a 'G;' after 'x;' in the next 3 scripts below
210 sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'
211
212 # print paragraph if it contains AAA and BBB and CCC (in any order)
213 sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'
214
215 # print paragraph if it contains AAA or BBB or CCC
216 sed -e '/./{H;$!d;}' -e 'x;/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d
217 gsed '/./{H;$!d;};x;/AAA\|BBB\|CCC/b;d' # GNU sed only
218
219 # print only lines of 65 characters or longer
220 sed -n '/^.\{65\}/p'
221
222 # print only lines of less than 65 characters
223 sed -n '/^.\{65\}/!p' # method 1, corresponds to above
224 sed '/^.\{65\}/d' # method 2, simpler syntax
225
226 # print section of file from regular expression to end of file
227 sed -n '/regexp/,$p'
228
229 # print section of file based on line numbers (lines 8-12, inclusive)
230 sed -n '8,12p' # method 1
231 sed '8,12!d' # method 2
232
233 # print line number 52
234 sed -n '52p' # method 1
235 sed '52!d' # method 2
236 sed '52q;d' # method 3, efficient on large files
237
238 # beginning at line 3, print every 7th line
239 gsed -n '3~7p' # GNU sed only
240 sed -n '3,${p;n;n;n;n;n;n;}' # other seds
241
242 # print section of file between two regular expressions (inclusive)
243 sed -n '/Iowa/,/Montana/p' # case sensitive
244
245SELECTIVE DELETION OF CERTAIN LINES:
246
247 # print all of file EXCEPT section between 2 regular expressions
248 sed '/Iowa/,/Montana/d'
249
250 # delete duplicate, consecutive lines from a file (emulates "uniq").
251 # First line in a set of duplicate lines is kept, rest are deleted.
252 sed '$!N; /^\(.*\)\n\1$/!P; D'
253
254 # delete duplicate, nonconsecutive lines from a file. Beware not to
255 # overflow the buffer size of the hold space, or else use GNU sed.
256 sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'
257
258 # delete all lines except duplicate lines (emulates "uniq -d").
259 sed '$!N; s/^\(.*\)\n\1$/\1/; t; D'
260
261 # delete the first 10 lines of a file
262 sed '1,10d'
263
264 # delete the last line of a file
265 sed '$d'
266
267 # delete the last 2 lines of a file
268 sed 'N;$!P;$!D;$d'
269
270 # delete the last 10 lines of a file
271 sed -e :a -e '$d;N;2,10ba' -e 'P;D' # method 1
272 sed -n -e :a -e '1,10!{P;N;D;};N;ba' # method 2
273
274 # delete every 8th line
275 gsed '0~8d' # GNU sed only
276 sed 'n;n;n;n;n;n;n;d;' # other seds
277
278 # delete lines matching pattern
279 sed '/pattern/d'
280
281 # delete ALL blank lines from a file (same as "grep '.' ")
282 sed '/^$/d' # method 1
283 sed '/./!d' # method 2
284
285 # delete all CONSECUTIVE blank lines from file except the first; also
286 # deletes all blank lines from top and end of file (emulates "cat -s")
287 sed '/./,/^$/!d' # method 1, allows 0 blanks at top, 1 at EOF
288 sed '/^$/N;/\n$/D' # method 2, allows 1 blank at top, 0 at EOF
289
290 # delete all CONSECUTIVE blank lines from file except the first 2:
291 sed '/^$/N;/\n$/N;//D'
292
293 # delete all leading blank lines at top of file
294 sed '/./,$!d'
295
296 # delete all trailing blank lines at end of file
297 sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' # works on all seds
298 sed -e :a -e '/^\n*$/N;/\n$/ba' # ditto, except for gsed 3.02.*
299
300 # delete the last line of each paragraph
301 sed -n '/^$/{p;h;};/./{x;/./p;}'
302
303SPECIAL APPLICATIONS:
304
305 # remove nroff overstrikes (char, backspace) from man pages. The 'echo'
306 # command may need an -e switch if you use Unix System V or bash shell.
307 sed "s/.`echo \\\b`//g" # double quotes required for Unix environment
308 sed 's/.^H//g' # in bash/tcsh, press Ctrl-V and then Ctrl-H
309 sed 's/.\x08//g' # hex expression for sed 1.5, GNU sed, ssed
310
311 # get Usenet/e-mail message header
312 sed '/^$/q' # deletes everything after first blank line
313
314 # get Usenet/e-mail message body
315 sed '1,/^$/d' # deletes everything up to first blank line
316
317 # get Subject header, but remove initial "Subject: " portion
318 sed '/^Subject: */!d; s///;q'
319
320 # get return address header
321 sed '/^Reply-To:/q; /^From:/h; /./d;g;q'
322
323 # parse out the address proper. Pulls out the e-mail address by itself
324 # from the 1-line return address header (see preceding script)
325 sed 's/ *(.*)//; s/>.*//; s/.*[:<] *//'
326
327 # add a leading angle bracket and space to each line (quote a message)
328 sed 's/^/> /'
329
330 # delete leading angle bracket & space from each line (unquote a message)
331 sed 's/^> //'
332
333 # remove most HTML tags (accommodates multiple-line tags)
334 sed -e :a -e 's/<[^>]*>//g;/</N;//ba'
335
336 # extract multi-part uuencoded binaries, removing extraneous header
337 # info, so that only the uuencoded portion remains. Files passed to
338 # sed must be passed in the proper order. Version 1 can be entered
339 # from the command line; version 2 can be made into an executable
340 # Unix shell script. (Modified from a script by Rahul Dhesi.)
341 sed '/^end/,/^begin/d' file1 file2 ... fileX | uudecode # vers. 1
342 sed '/^end/,/^begin/d' "$@" | uudecode # vers. 2
343
344 # sort paragraphs of file alphabetically. Paragraphs are separated by blank
345 # lines. GNU sed uses \v for vertical tab, or any unique char will do.
346 sed '/./{H;d;};x;s/\n/={NL}=/g' file | sort | sed '1s/={NL}=//;s/={NL}=/\n/g'
347 gsed '/./{H;d};x;y/\n/\v/' file | sort | sed '1s/\v//;y/\v/\n/'
348
349 # zip up each .TXT file individually, deleting the source file and
350 # setting the name of each .ZIP file to the basename of the .TXT file
351 # (under DOS: the "dir /b" switch returns bare filenames in all caps).
352 echo @echo off >zipup.bat
353 dir /b *.txt | sed "s/^\(.*\)\.TXT/pkzip -mo \1 \1.TXT/" >>zipup.bat
354
355TYPICAL USE: Sed takes one or more editing commands and applies all of
356them, in sequence, to each line of input. After all the commands have
357been applied to the first input line, that line is output and a second
358input line is taken for processing, and the cycle repeats. The
359preceding examples assume that input comes from the standard input
360device (i.e, the console, normally this will be piped input). One or
361more filenames can be appended to the command line if the input does
362not come from stdin. Output is sent to stdout (the screen). Thus:
363
364 cat filename | sed '10q' # uses piped input
365 sed '10q' filename # same effect, avoids a useless "cat"
366 sed '10q' filename > newfile # redirects output to disk
367
368For additional syntax instructions, including the way to apply editing
369commands from a disk file instead of the command line, consult "sed &
370awk, 2nd Edition," by Dale Dougherty and Arnold Robbins (O'Reilly,
3711997; http://www.ora.com), "UNIX Text Processing," by Dale Dougherty
372and Tim O'Reilly (Hayden Books, 1987) or the tutorials by Mike Arst
373distributed in U-SEDIT2.ZIP (many sites). To fully exploit the power
374of sed, one must understand "regular expressions." For this, see
375"Mastering Regular Expressions" by Jeffrey Friedl (O'Reilly, 1997).
376The manual ("man") pages on Unix systems may be helpful (try "man
377sed", "man regexp", or the subsection on regular expressions in "man
378ed"), but man pages are notoriously difficult. They are not written to
379teach sed use or regexps to first-time users, but as a reference text
380for those already acquainted with these tools.
381
382QUOTING SYNTAX: The preceding examples use single quotes ('...')
383instead of double quotes ("...") to enclose editing commands, since
384sed is typically used on a Unix platform. Single quotes prevent the
385Unix shell from intrepreting the dollar sign ($) and backquotes
386(`...`), which are expanded by the shell if they are enclosed in
387double quotes. Users of the "csh" shell and derivatives will also need
388to quote the exclamation mark (!) with the backslash (i.e., \!) to
389properly run the examples listed above, even within single quotes.
390Versions of sed written for DOS invariably require double quotes
391("...") instead of single quotes to enclose editing commands.
392
393USE OF '\t' IN SED SCRIPTS: For clarity in documentation, we have used
394the expression '\t' to indicate a tab character (0x09) in the scripts.
395However, most versions of sed do not recognize the '\t' abbreviation,
396so when typing these scripts from the command line, you should press
397the TAB key instead. '\t' is supported as a regular expression
398metacharacter in awk, perl, and HHsed, sedmod, and GNU sed v3.02.80.
399
400VERSIONS OF SED: Versions of sed do differ, and some slight syntax
401variation is to be expected. In particular, most do not support the
402use of labels (:name) or branch instructions (b,t) within editing
403commands, except at the end of those commands. We have used the syntax
404which will be portable to most users of sed, even though the popular
405GNU versions of sed allow a more succinct syntax. When the reader sees
406a fairly long command such as this:
407
408 sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d
409
410it is heartening to know that GNU sed will let you reduce it to:
411
412 sed '/AAA/b;/BBB/b;/CCC/b;d' # or even
413 sed '/AAA\|BBB\|CCC/b;d'
414
415In addition, remember that while many versions of sed accept a command
416like "/one/ s/RE1/RE2/", some do NOT allow "/one/! s/RE1/RE2/", which
417contains space before the 's'. Omit the space when typing the command.
418
419OPTIMIZING FOR SPEED: If execution speed needs to be increased (due to
420large input files or slow processors or hard disks), substitution will
421be executed more quickly if the "find" expression is specified before
422giving the "s/.../.../" instruction. Thus:
423
424 sed 's/foo/bar/g' filename # standard replace command
425 sed '/foo/ s/foo/bar/g' filename # executes more quickly
426 sed '/foo/ s//bar/g' filename # shorthand sed syntax
427
428On line selection or deletion in which you only need to output lines
429from the first part of the file, a "quit" command (q) in the script
430will drastically reduce processing time for large files. Thus:
431
432 sed -n '45,50p' filename # print line nos. 45-50 of a file
433 sed -n '51q;45,50p' filename # same, but executes much faster
434
435If you have any additional scripts to contribute or if you find errors
436in this document, please send e-mail to the compiler. Indicate the
437version of sed you used, the operating system it was compiled for, and
438the nature of the problem. To qualify as a one-liner, the command line
439must be 65 characters or less. Various scripts in this file have been
440written or contributed by:
441
442 Al Aab # founder of "seders" list
443 Edgar Allen # various
444 Yiorgos Adamopoulos # various
445 Dale Dougherty # author of "sed & awk"
446 Carlos Duarte # author of "do it with sed"
447 Eric Pement # author of this document
448 Ken Pizzini # author of GNU sed v3.02
449 S.G. Ravenhall # great de-html script
450 Greg Ubben # many contributions & much help
451-------------------------------------------------------------------------