sed (Stream Editor)

本文详细介绍了SED文本流编辑器的基本用法及高级技巧,包括常用选项、命令解释与实例演示。SED可用于过滤和转换文本,特别适合用于自动化脚本中。

sed [-n] [-e] ‘command(s)’ files
sed [-n] -f scriptfile files

1. Options
-n, –quiet, –silent
-e script, –expression=script
-i[SUFFIX], –in-place[=SUFFIX]
-r, –regexp-extended

-f script-myfile, –myfile=script-myfile

2. Commands
d delete pattern space, start next cycle
p print the current pattern space

sed '3d' myfile
sed '3,$d' myfile
sed '/line/'d myfile

# delete spaces in front of each row
sed 's/^[ ]*//g' myfile         
sed 's/^ *//g' myfile       
sed 's/^[[:space:]]*//g' myfile   # inlcude TAB

# grep def
echo "abc\ndef\nghi\njkl" | sed -n '/def/p'
echo "abc\ndef\nghi\njkl" | sed '/def/!d'

s/regexp/replacement/ replace that portion matched with replacement

echo "abcdefg" | sed 's/abc/123/g'        # 123defg
echo "abcdefg" | sed 's#\(abc\)defg#\1#'  # abc

r filename append text read from filename.

cat newfile             # 123\n456\n
echo "abc\ndef" | sed '/abc/r newfile'   # abc\n123\n456\ndef

w filename write the current pattern space to filename.

echo "abc\ndef" | sed -n '/abc/w newfile'  # now, newfile content is "abc"

a\ text append text

echo "abc\ndef" | sed '/^abc/a\123'  # "abc\n123\ndef"

i\ text insert text

echo "abc\ndef" | sed '/^abc/i\123' # “123\nabc\ndef"

h H copy/append pattern space to hold space.
g G copy/append hold space to pattern space.
x exchange the contents of the hold and pattern spaces.
! not

# echo "abc\ndef\nghi" | sed -e '/def/h' -e '$G'  "abc\ndef\nghi\ndef"

# echo "abc\ndef\nghi" | sed -e '/def/H' -e '$G'  "abc\ndef\nghi\n\ndef"

# echo "abc\ndef\nghi" | sed -e '/def/h' -e '$g'
"abc\ndef\ndef"

# echo "abc\ndef\nghi" | sed -e '/def/H' -e '$g'
"abc\ndef\n\ndef"

# echo "abc\ndef\nghi\njkl" | sed '1!G;h;$!d'
"jkl\nghi\ndef\nabc"

# echo "abc\ndef" | sed G       
"abc\n\ndef\n"

# echo "abc\n\n\ndef" | sed '/^$/d;G'  
"abc\n\ndef\n"

# echo "abc\ndef\nghi\njkl" | sed '/def/{x;p;x;}' 
"abc\n\ndef\nghi\njkl", # add a blank line before the matching one

n N read/append the next line of input into the pattern space.

echo "abc\ndef" | sed '/abc/{n;s/e/E/;}'    #  "abc\ndEf"
echo "abc\ndef\nghi\njkl" | sed 'n;d'       #  "abc\nghi", delete the even lines

y/source/dest/ Transliterate the characters in the pattern space which appear in source to the corresponding character in dest.

echo "abc\ndef" | sed '1y/abcdef/ABCDEF/'   #  "ABC\ndef"   

= print the current line number.

echo "abc\ndef\nghi\njkl" | sed -n '/def/='     #  2
echo "abc\ndef\nghi\njkl" | sed -n '$='        #  4   <=> wc -l

q immediately quit the sed script without processing any more input

echo "abc\ndef\nghi\njkl" | sed 2q  #  "abc\ndef"  <=> head -2
echo "abc\ndef\nghi\njkl" | sed -e :a -e '$q;N;2,$D;ba' 

Examples:
### Labels ##########################
:label
:start
:end
:up

[eli@localhost ~]$ cat books.txt 
Storm of Swords, George R. R. Martin, 1216 
Two Towers, J. R. R. Tolkien, 352 
Alchemist, Paulo Coelho, 197 
The Fellowship of the Ring, J. R. R. Tolkien, 432 
The Pilgrimage, Paulo Coelho, 288 
A Game of Thrones, George R. R. Martin, 864

# GOTO

[eli@localhost ~]$ sed -n ' 
> h;n;H;x 
> s/\n/, / 
> /Paulo/!b Print 
> s/^/- / 
> :Print 
> p' books.txt
Storm of Swords, George R. R. Martin, 1216 , Two Towers, J. R. R. Tolkien, 352 
- Alchemist, Paulo Coelho, 197 , The Fellowship of the Ring, J. R. R. Tolkien, 432 
- The Pilgrimage, Paulo Coelho, 288 , A Game of Thrones, George R. R. Martin, 864

[eli@localhost ~]$ sed -n 'h;n;H;x;s/\n/, /;/Paulo/!b Print; s/^/- /; :Print;p' books.txt 
Storm of Swords, George R. R. Martin, 1216 , Two Towers, J. R. R. Tolkien, 352 
- Alchemist, Paulo Coelho, 197 , The Fellowship of the Ring, J. R. R. Tolkien, 432 
- The Pilgrimage, Paulo Coelho, 288 , A Game of Thrones, George R. R. Martin, 864

# LOOP
**[eli@localhost ~]$ sed -n ’

h;n;H;x
s/\n/, /
:Loop
/Paulo/s/^/-/
/—-/!t Loop
p’ books.txt
Storm of Swords, George R. R. Martin, 1216 , Two Towers, J. R. R. Tolkien, 352
—-Alchemist, Paulo Coelho, 197 , The Fellowship of the Ring, J. R. R. Tolkien, 432
—-The Pilgrimage, Paulo Coelho, 288 , A Game of Thrones, George R. R. Martin, 864
[eli@localhost ~]$ sed -n ‘h;n;H;x; s/\n/, /; :Loop;/Paulo/s/^/-/; /—-/!t Loop; p’ books.txt
Storm of Swords, George R. R. Martin, 1216 , Two Towers, J. R. R. Tolkien, 352
—-Alchemist, Paulo Coelho, 197 , The Fellowship of the Ring, J. R. R. Tolkien, 432
—-The Pilgrimage, Paulo Coelho, 288 , A Game of Thrones, George R. R. Martin, 864**

# M,+n, print the next n lines starting from line number M

[eli@localhost ~]$ sed -n '2,+3 p' books.txt 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 

# M~n, start at line number M and process every n(th) line

[eli@localhost ~]$ sed -n '2~3 p' books.txt 
2) The Two Towers, J. R. R. Tolkien, 352 
5) The Pilgrimage, Paulo Coelho, 288 

[eli@localhost ~]$ sed -n '2~2 p' books.txt 
2) The Two Towers, J. R. R. Tolkien, 352 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
6) A Game of Thrones, George R. R. Martin, 864

### delete command #####################################
[address1[,address2]]d

[eli@localhost ~]$ sed '2d' books.txt
1) A Storm of Swords, George R. R. Martin, 1216 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864 

### write command #################################
[address1[,address2]]w file

[eli@localhost ~]$ sed -n '2~2 w junk.txt' books.txt 
[eli@localhost ~]$ cat junk.txt 
2) The Two Towers, J. R. R. Tolkien, 352 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
6) A Game of Thrones, George R. R. Martin, 864 

### append command ####################################
[address]a\
Append text

[eli@localhost ~]$ sed '4 a 7) Adultry, Paulo Coelho, 234' books.txt
1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
7) Adultry, Paulo Coelho, 234
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864 

### change/replace command ##############################
[address1[,address2]]c\
Replace text

[eli@localhost ~]$ sed '3 c 3) Adultry, Paulo Coelho, 234' books.txt
1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) Adultry, Paulo Coelho, 234
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864  

### insert command ##############################
[address]i\ Insert text

[eli@localhost ~]$ sed '4 i 7) Adultry, Paulo Coelho, 234' books.txt
1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
7) Adultry, Paulo Coelho, 234
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864 

### translate command ##############################
[address1[,address2]]y/list-1/list-2/

[eli@localhost ~]$ echo "1 5 15 20" | sed 'y/151520/IVXVXX/'
I V IV XX

### l command #################################
display hidden characters
[address1[,address2]]l
[address1[,address2]]l [len]

[eli@localhost ~]$ sed 's/ /\t/g' books.txt > junk.txt

[eli@localhost ~]$ cat -A junk.txt 
1)^IA^IStorm^Iof^ISwords,^IGeorge^IR.^IR.^IMartin,^I1216^I$
2)^IThe^ITwo^ITowers,^IJ.^IR.^IR.^ITolkien,^I352^I$
3)^IThe^IAlchemist,^IPaulo^ICoelho,^I197^I$
4)^IThe^IFellowship^Iof^Ithe^IRing,^IJ.^IR.^IR.^ITolkien,^I432^I$
5)^IThe^IPilgrimage,^IPaulo^ICoelho,^I288^I$
6)^IA^IGame^Iof^IThrones,^IGeorge^IR.^IR.^IMartin,^I864^I$

[eli@localhost ~]$ sed -n 'l' junk.txt 
1)\tA\tStorm\tof\tSwords,\tGeorge\tR.\tR.\tMartin,\t1216\t$
2)\tThe\tTwo\tTowers,\tJ.\tR.\tR.\tTolkien,\t352\t$
3)\tThe\tAlchemist,\tPaulo\tCoelho,\t197\t$
4)\tThe\tFellowship\tof\tthe\tRing,\tJ.\tR.\tR.\tTolkien,\t432\t$
5)\tThe\tPilgrimage,\tPaulo\tCoelho,\t288\t$
6)\tA\tGame\tof\tThrones,\tGeorge\tR.\tR.\tMartin,\t864\t$

[eli@localhost ~]$ sed -n 'l 25' books.txt
1) A Storm of Swords, Ge\
orge R. R. Martin, 1216 $
2) The Two Towers, J. R.\
 R. Tolkien, 352 $
3) The Alchemist, Paulo \
Coelho, 197 $
4) The Fellowship of the\
 Ring, J. R. R. Tolkien,\
 432 $
5) The Pilgrimage, Paulo\
 Coelho, 288 $
6) A Game of Thrones, Ge\
orge R. R. Martin, 864 $

### quit command ###############################
[address]q
[address]q [value]

[eli@localhost ~]$ sed '3 q' books.txt
1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 

[eli@localhost ~]$ sed '3 q 100' books.txt
1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
[eli@localhost ~]$ echo $?
100

### read command #################################
[address]r file

[

eli@localhost ~]$ echo "this is junk text" > junk.txt
[eli@localhost ~]$ sed '3 r junk.txt' books.txt
1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
this is junk text
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864 

[eli@localhost ~]$ sed '3,5 r junk.txt' books.txt
1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
this is junk text
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
this is junk text
5) The Pilgrimage, Paulo Coelho, 288 
this is junk text
6) A Game of Thrones, George R. R. Martin, 864 

### execute command #################################
[address1[,address2]]e [command]

[eli@localhost ~]$ sed '3 e date' books.txt
1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
Sat Jun  6 06:09:31 PDT 2015
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864 

### = command ###############################
writes the line number
[/pattern/]=
[address1[,address2]]=

[eli@localhost ~]$ sed '=' books.txt
1
1) A Storm of Swords, George R. R. Martin, 1216 
2
2) The Two Towers, J. R. R. Tolkien, 352 
3
3) The Alchemist, Paulo Coelho, 197 
4
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5
5) The Pilgrimage, Paulo Coelho, 288 
6
6) A Game of Thrones, George R. R. Martin, 864 

[eli@localhost ~]$ sed '2,3=' books.txt
1) A Storm of Swords, George R. R. Martin, 1216 
2
2) The Two Towers, J. R. R. Tolkien, 352 
3
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864 

[eli@localhost ~]$ sed '/Paulo/ =' books.txt 
1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864 

### & command ###########################
stores the matched pattern

[eli@localhost ~]$ sed 's/[[:digit:]]/Book number &/' books.txt
Book number 1) A Storm of Swords, George R. R. Martin, 1216 
Book number 2) The Two Towers, J. R. R. Tolkien, 352 
Book number 3) The Alchemist, Paulo Coelho, 197 
Book number 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
Book number 5) The Pilgrimage, Paulo Coelho, 288 
Book number 6) A Game of Thrones, George R. R. Martin, 864 

[eli@localhost ~]$ sed 's/[[:digit:]]* $/Pages = &/' books.txt
1) A Storm of Swords, George R. R. Martin, Pages = 1216 
2) The Two Towers, J. R. R. Tolkien, Pages = 352 
3) The Alchemist, Paulo Coelho, Pages = 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, Pages = 432 
5) The Pilgrimage, Paulo Coelho, Pages = 288 
6) A Game of Thrones, George R. R. Martin, Pages = 864 

### subsitute command ##############################
[address1[,address2]]s/pattern/replacement/[flags]

[eli@localhost ~]$ sed 's/,/ | /g' books.txt
1) A Storm of Swords |  George R. R. Martin |  1216 
2) The Two Towers |  J. R. R. Tolkien |  352 
3) The Alchemist |  Paulo Coelho |  197 
4) The Fellowship of the Ring |  J. R. R. Tolkien |  432 
5) The Pilgrimage |  Paulo Coelho |  288 
6) A Game of Thrones |  George R. R. Martin |  864 

### create substring ##########################

[eli@localhost ~]$ echo "One Two Three" | sed 's#\(\w+\) \(\w+\) \(\w+\)#\3 \2 \1#'
One Two Three

### managering patterns ########################
n command prints the contents of the pattern buffer, clears the pattern buffer, fetches the next line into the pattern buffer, and applies commands on it.
[address1[,address2]]n

x command exchanges the contents of pattern and hold buffers

[eli@localhost ~]$ cat books.txt
A Storm of Swords 
George R. R. Martin 
The Two Towers 
J. R. R. Tolkien 
The Alchemist 
Paulo Coelho 
The Fellowship of the Ring 
J. R. R. Tolkien 
The Pilgrimage 
Paulo Coelho 
A Game of Thrones 
George R. R. Martin 

[eli@localhost ~]$ sed -n 'x;n;p' books.txt
George R. R. Martin 
J. R. R. Tolkien 
Paulo Coelho 
J. R. R. Tolkien 
Paulo Coelho 
George R. R. Martin 

[eli@localhost ~]$ sed -n 'x;n;x;p' books.txt
A Storm of Swords 
The Two Towers 
The Alchemist 
The Fellowship of the Ring 
The Pilgrimage 
A Game of Thrones 

h command which copies data from the pattern buffer to the hold buffer
[address1[,address2]]h

[eli@localhost ~]$ sed -n '/Paulo/!h; /Paulo/{x;p}' books.txt
The Alchemist 
The Pilgrimage 

H command which appends the contents to the hold buffer by adding a new line at the end
[address1[,address2]]H

[eli@localhost ~]$ sed -n '/Paulo/!h; /Paulo/{H;x;p}' books.txt
The Alchemist 
Paulo Coelho 
The Pilgrimage 
Paulo Coelho 

g command which copies data from the hold buffer to the pattern buffer
[address1[,address2]]g

[eli@localhost ~]$ sed -n '/Paulo/!h; /Paulo/{p;g;p}' books.txt
Paulo Coelho 
The Alchemist 
Paulo Coelho 
The Pilgrimage 

G command which appends the contents to the pattern buffer by adding a new line at the end
[address1[,address2]]G

[eli@localhost ~]$ sed -n '/Paulo/!h; /Paulo/{G;p}' books.txt
Paulo Coelho 
The Alchemist 
Paulo Coelho 
The Pilgrimage 

### Regex ##############################
^: start of line
$: end of line
.: single character
[]: character set
[^]: exclusive set
[-]: character range
\?: zero or one occurrence
+: one or more occurrences
*: zero or more occurrences
{n}: exactly N occurrences
{n,}: at least N occurrences
{m,n}: M to N occurrences
|: or

: escaping
\n: new line character
\r: carriage return
\dnnn: a character whose decimal ASCII value is nnn
\onnn: a character whose octal ASCII value is nnn
\xnn: character whose hexadecimal ASCII value is nnn

POSIX
[:alnum:]
[:alpha:]
[:blank:] # space or tab
[:digit:]
[:lower:]
[:upper:]
[:punct:]
[:space:]

\b: word boundary
\B: non-word boundary

\s: single whitespace
\S: non-single whitespace

\w: single word character
\W: non-single word character

[eli@localhost ~]$ echo -e "a\nb\nc" | sed -n '/\d97/ p'
a

[eli@localhost ~]$ echo -e "a\nb\nc" | sed -n '/\o142/ p'
b

[eli@localhost ~]$ echo -e "a\nb\nc" | sed -n '/\x63/ p'
c

### equivalent command #############
# cat

sed '' books.txt
sed -n 'p' books.txt

# removing empty lines

echo -e "Line #1\n\n\nLine #2" | sed -n '/^$/!p'
echo -e "Line #1\n\n\nLine #2" | sed  '/^$/d'

# adding comments before certain lines

sed '3,5 s/^/#/' hello.sh

# wc -l

sed -n '$ =' books.txt

# head

sed '10 q' books.txt

# tail -1

sed -n '$ p' books.txt

# dos2unix

file text.txt
test.txt: ASCII text, with CRLF line terminators 

sed 's/^M$//' test.txt > new.txt  # ctrl-v, ctrl-m

# nl

echo -e "Line1\nLine2\nLine3" | sed '=' | sed 'N;s/\n/\t/'

# expand, convert TAB to whitespaces

echo -e "One\tTwo\tThree" | sed 's/\t/    /'

# tee

sed -n 'p; w new.txt' test.txt

# cat -s, supresses repeated empty output lines to one empty line

echo -e "Line1\n\nLine2\n\n\nLine3\n" | sed '1s/^$//p; /./,/^$/!d'

# grep

sed -n '/Paulo/ p' books.txt

# tr

echo "abc" | sed 'y/abc/ABC/'

参考资料 http://www.tutorialspoint.com/sed/index.htm

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值