"http://c.biancheng.net"
,它很有可能出現在兩行中,每行各包含其中一部分。這時,如果用普通的 sed 編輯器命令來處理文字,就不可能發現這種被分開的情況。注意,以上命令的縮寫,都為大寫。
[[email protected] ~]# cat data2.txt
This is the header line.
This is the first data line.
This is the second data line.
This is the last line.
[[email protected] ~]# sed '/first/{ N ; s/n/ / }' data2.txt
This is the header line.
This is the first data line. This is the second data line.
This is the last line.
[[email protected] ~]# cat data3.txt
On Tuesday, the Linux System
Administrator's group meeting will be held.
All System Administrators should attend.
Thank you for your attendance.
[[email protected] ~]# sed 'N ; s/System Administrator/Desktop User/' data3.txt
On Tuesday, the Linux Desktop User's group meeting will be held.
All Desktop Users should attend.
Thank you for your attendance.
[[email protected] ~]# sed 'N
> s/SystemnAdministrator/DesktopnUser/
> s/System Administrator/Desktop User/
> ' data3.txt
On Tuesday, the Linux Desktop
User's group meeting will be held.
All Desktop Users should attend.
Thank you for your attendance.
[[email protected] ~]# sed '
> s/SystemnAdministrator/DesktopnUser/
> N
> s/System Administrator/Desktop User/
> ' data3.txt
On Tuesday, the Linux Desktop
User's group meeting will be held.
All Desktop Users should attend.
Thank you for your attendance.
[[email protected] ~]# cat data4.txt
On Tuesday, the Linux System
Administrator's group meeting will be held.
All System Administrators should attend.
[[email protected] ~]# sed 'N ; /SystemnAdministrator/D' data4.txt
Administrator's group meeting will be held.
All System Administrators should attend.
[[email protected] ~]# cat data5.txt
This is the header line.
This is a data line.
This is the last line.
[[email protected] ~]# sed '/^$/{N ; /header/D}' data5.txt
This is the header line.
This is a data line.
This is the last line.
[[email protected] ~]# cat test.txt
aaa
bbb
ccc
ddd
eee
fff
P(大寫)命令 | p(小寫)命令 |
---|---|
[[email protected] ~]# sed '/.*/N;P' |
[[email protected] ~]# sed '/.*/N;p' |
命令 | 功能 |
---|---|
h | 將模式空間中的內容複製到保持空間 |
H | 將模式空間中的內容附加到保持空間 |
g | 將保持空間中的內容複製到模式空間 |
G | 將保持空間中的內容附加到模式空間 |
x | 交換模式空間和保持空間中的內容 |
[[email protected] ~]# cat data2.txt
This is the header line.
This is the first data line.
This is the second data line.
This is the last line.
[[email protected] ~]# sed -n '/first/ {h ; p ; n ; p ; g ; p }' data2.txt
This is the first data line.
This is the second data line.
This is the first data line.
[address]b [label]
其中,address 引數決定了哪些行的資料會觸發分支命令,label 引數定義了要跳轉到的位置。
[[email protected] ~]# cat data2.txt
This is the header line.
This is the first data line.
This is the second data line.
This is the last line.
[[email protected] ~]# sed '{2,3b ; s/This is/Is this/ ; s/line./test?/}' data2.txt
Is this the header test?
This is the first data line.
This is the second data line.
Is this the last test?
[[email protected] ~]# sed '{/first/b jump1 ; s/This is the/No jump on/
> :jump1
> s/This is the/Jump here on/}' data2.txt
No jump on header line
Jump here on first data line
No jump on second data line
No jump on last line
[[email protected] ~]# echo "This, is, a, test, to, remove, commas." | sed -n '{
> :start
> s/,//1p
> /,/b start
> }'
This is, a, test, to, remove, commas.
This is a, test, to, remove, commas.
This is a test, to, remove, commas.
This is a test to, remove, commas.
This is a test to remove, commas.
This is a test to remove commas.
[address]t [label]
跟分支命令一樣,在沒有指定標籤的情況下,如果 s 命令替換成功,sed 會跳轉到指令碼的結尾(相當於不執行任何指令碼命令)。例如:
[[email protected] ~]# sed '{
> s/first/matched/
> t
> s/This is the/No match on/
> }' data2.txt
No match on header line
This is the matched data line
No match on second data line
No match on last line
[[email protected] ~]# echo "This, is, a, test, to, remove, commas. " | sed -n '{
> :start
> s/,//1p
> t start
> }'
This is, a, test, to, remove, commas.
This is a, test, to, remove, commas.
This is a test, to, remove, commas.
This is a test to, remove, commas.
This is a test to remove, commas.
This is a test to remove commas.