📄 451.html
字号:
<br>
# Cannot be done with DOS versions of sed. Use "tr" instead.
<br>
tr -d
<infile >outfile # GNU tr version 1.22 or higher
<br>
<br>
# delete leading whitespace (spaces, tabs) from front of each line
<br>
# aligns all text flush left
<br>
sed 's/^[ ]*//' # see note on ' ' at end of file
<br>
<br>
# delete trailing whitespace (spaces, tabs) from end of each line
<br>
sed 's/[ ]*$//' # see note on ' ' at end of file
<br>
<br>
# delete BOTH leading and trailing whitespace from each line
<br>
sed 's/^[ ]*//;s/[ ]*$//'
<br>
<br>
# insert 5 blank spaces at beginning of each line (make page offset)
<br>
sed 's/^/ /'
<br>
<br>
# align all text flush right on a 79-column width
<br>
sed -e :a -e 's/^.{1,78}$/ &/;ta' # set at 78 plus 1 space
<br>
<br>
# center all text in the middle of 79-column width. In method 1,
<br>
# spaces at the beginning of the line are significant, and trailing
<br>
# spaces are appended at the end of the line. In method 2, spaces at
<br>
# the beginning of the line are discarded in centering the line, and
<br>
# no trailing spaces appear at the end of lines.
<br>
sed -e :a -e 's/^.{1,77}$/ & /;ta' # method 1
<br>
sed -e :a -e 's/^.{1,77}$/ &/;ta' -e 's/( *)1/1/' # method 2
<br>
<br>
# substitute (find and replace) "foo" with "bar" on each line
<br>
sed 's/foo/bar/' # replaces only 1st instance in a line
<br>
sed 's/foo/bar/4' # replaces only 4th instance in a line
<br>
sed 's/foo/bar/g' # replaces ALL instances in a line
<br>
sed 's/(.*)foo(.*foo)/1bar2/' # replace the next-to-last case
<br>
sed 's/(.*)foo/1bar/' # replace only the last case
<br>
<br>
# substitute "foo" with "bar" ONLY for lines which contain "baz"
<br>
sed '/baz/s/foo/bar/g'
<br>
<br>
# substitute "foo" with "bar" EXCEPT for lines which contain "baz"
<br>
sed '/baz/!s/foo/bar/g'
<br>
<br>
# change "scarlet" or "ruby" or "puce" to "red"
<br>
sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g' # most seds
<br>
gsed 's/scarlet|ruby|puce/red/g' # GNU sed only
<br>
<br>
# reverse order of lines (emulates "tac")
<br>
# bug/feature in HHsed v1.5 causes blank lines to be deleted
<br>
sed '1!G;h;$!d' # method 1
<br>
sed -n '1!G;h;$p' # method 2
<br>
<br>
# reverse each character on the line (emulates "rev")
<br>
sed '//!G;s/(.)(.*)/&21/;//D;s/.//'
<br>
<br>
# join pairs of lines side-by-side (like "paste")
<br>
sed '$!N;s// /'
<br>
<br>
# if a line ends with a backslash, append the next line to it
<br>
sed -e :a -e '/$/N; s///; ta'
<br>
<br>
# if a line begins with an equal sign, append it to the previous line
<br>
# and replace the "=" with a single space
<br>
sed -e :a -e '$!N;s/=/ /;ta' -e 'P;D'
<br>
<br>
# add commas to numeric strings, changing "1234567" to "1,234,567"
<br>
gsed ':a;s/B[0-9]{3}>/,&/;ta' # GNU sed
<br>
sed -e :a -e 's/(.*[0-9])([0-9]{3})/1,2/;ta' # other seds
<br>
<br>
# add commas to numbers with decimal points and minus signs (GNU sed)
<br>
gsed ':a;s/(^|[^0-9.])([0-9]+)([0-9]{3})/12,3/g;ta'
<br>
<br>
# add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)
<br>
gsed '0~5G' # GNU sed only
<br>
sed 'n;n;n;n;G;' # other seds
<br>
<br>
SELECTIVE PRINTING OF CERTAIN LINES:
<br>
<br>
# print first 10 lines of file (emulates behavior of "head")
<br>
sed 10q
<br>
<br>
# print first line of file (emulates "head -1")
<br>
sed q
<br>
<br>
# print the last 10 lines of a file (emulates "tail")
<br>
sed -e :a -e '$q;N;11,$D;ba'
<br>
<br>
# print the last 2 lines of a file (emulates "tail -2")
<br>
sed '$!N;$!D'
<br>
<br>
# print the last line of a file (emulates "tail -1")
<br>
sed '$!d' # method 1
<br>
sed -n '$p' # method 2
<br>
<br>
# print only lines which match regular expression (emulates "grep")
<br>
sed -n '/regexp/p' # method 1
<br>
sed '/regexp/!d' # method 2
<br>
<br>
# print only lines which do NOT match regexp (emulates "grep -v")
<br>
sed -n '/regexp/!p' # method 1, corresponds to above
<br>
sed '/regexp/d' # method 2, simpler syntax
<br>
<br>
# print the line immediately before a regexp, but not the line
<br>
# containing the regexp
<br>
sed -n '/regexp/{g;1!p;};h'
<br>
<br>
# print the line immediately after a regexp, but not the line
<br>
# containing the regexp
<br>
sed -n '/regexp/{n;p;}'
<br>
<br>
# print 1 line of context before and after regexp, with line number
<br>
# indicating where the regexp occurred (similar to "grep -A1 -B1")
<br>
sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h
<br>
<br>
# grep for AAA and BBB and CCC (in any order)
<br>
sed '/AAA/!d; /BBB/!d; /CCC/!d'
<br>
<br>
# grep for AAA and BBB and CCC (in that order)
<br>
sed '/AAA.*BBB.*CCC/!d'
<br>
<br>
# grep for AAA or BBB or CCC (emulates "egrep")
<br>
sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d # most seds
<br>
gsed '/AAA|BBB|CCC/!d' # GNU sed only
<br>
<br>
# print paragraph if it contains AAA (blank lines separate paragraphs)
<br>
# HHsed v1.5 must insert a 'G;' after 'x;' in the next 3 scripts below
<br>
sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'
<br>
<br>
# print paragraph if it contains AAA and BBB and CCC (in any order)
<br>
sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'
<br>
<br>
# print paragraph if it contains AAA or BBB or CCC
<br>
sed -e '/./{H;$!d;}' -e 'x;/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d
<br>
gsed '/./{H;$!d;};x;/AAA|BBB|CCC/b;d' # GNU sed only
<br>
<br>
# print only lines of 65 characters or longer
<br>
sed -n '/^.{65}/p'
<br>
<br>
# print only lines of less than 65 characters
<br>
sed -n '/^.{65}/!p' # method 1, corresponds to above
<br>
sed '/^.{65}/d' # method 2, simpler syntax
<br>
<br>
# print section of file from regular expression to end of file
<br>
sed -n '/regexp/,$p'
<br>
<br>
# print section of file based on line numbers (lines 8-12, inclusive)
<br>
sed -n '8,12p' # method 1
<br>
sed '8,12!d' # method 2
<br>
<br>
# print line number 52
<br>
sed -n '52p' # method 1
<br>
sed '52!d' # method 2
<br>
sed '52q;d' # method 3, efficient on large files
<br>
<br>
# beginning at line 3, print every 7th line
<br>
gsed -n '3~7p' # GNU sed only
<br>
sed -n '3,${p;n;n;n;n;n;n;}' # other seds
<br>
<br>
# print section of file between two regular expressions (inclusive)
<br>
sed -n '/Iowa/,/Montana/p' # case sensitive
<br>
<br>
SELECTIVE DELETION OF CERTAIN LINES:
<br>
<br>
# print all of file EXCEPT section between 2 regular expressions
<br>
sed '/Iowa/,/Montana/d'
<br>
<br>
# delete duplicate, consecutive lines from a file (emulates "uniq").
<br>
# First line in a set of duplicate lines is kept, rest are deleted.
<br>
sed '$!N; /^(.*)1$/!P; D'
<br>
<br>
# delete duplicate, nonconsecutive lines from a file. Beware not to
<br>
# overflow the buffer size of the hold space, or else use GNU sed.
<br>
sed -n 'G; s//&&/; /^([ -~]*).*1/d; s///; h; P'
<br>
<br>
# delete the first 10 lines of a file
<br>
sed '1,10d'
<br>
<br>
# delete the last line of a file
<br>
sed '$d'
<br>
<br>
# delete the last 2 lines of a file
<br>
sed 'N;$!P;$!D;$d'
<br>
<br>
# delete the last 10 lines of a file
<br>
sed -e :a -e '$d;N;2,10ba' -e 'P;D' # method 1
<br>
sed -n -e :a -e '1,10!{P;N;D;};N;ba' # method 2
<br>
<br>
# delete every 8th line
<br>
gsed '0~8d' # GNU sed only
<br>
sed 'n;n;n;n;n;n;n;d;' # other seds
<br>
<br>
# delete ALL blank lines from a file (same as "grep '.' ")
<br>
sed '/^$/d' # method 1
<br>
sed '/./!d' # method 2
<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -