博客 / 詳情

返回

Linux命令—sed

作用:用於處理文本的流編輯器.

命令格式

sed Options '[Address]Command' InputFile

對輸入文件 InputFile 中的每行執行給定的命令 Command.

如果指定了地址 Address,則只對符合條件的文本行執行 Command 命令.

如果需要對同一行執行多條命令,則命令之間使用 ; 隔開即可.

常用選項

  • -i: 原地修改,會改變源文件中的內容.
  • -n: 處理過程中,不要輸出正在處理的文本行.
  • -r: 使用正則表達式擴展.
  • -f File: 從文件 File 中讀取 sed 命令.

地址

  • n: 第 n 行.
[ming@localhost test]$ cat data.txt 
a
a123
aa456
aaa789
b123
bb456
bbb789

[ming@localhost test]$ sed -n '2p' data.txt 
a123

p: 打印行.

  • $: 最後一行.
[ming@localhost test]$ sed -n '$p' data.txt 
bbb789
  • /regexp/: 匹配指定正則表達式的行.
[ming@localhost test]$ sed -nr '/a+/p' data.txt
a
a123
aa456
aaa789

[ming@localhost test]$ sed -nr '/a{2,}/p' data.txt
aa456
aaa789
  • n~m: 從第 n 行開始,每 m 行便執行給定的命令.
[ming@localhost test]$ cat data.txt 
a
a123
aa456
aaa789
b123
bb456
bbb789

[ming@localhost test]$ sed -n '1~2p' data.txt 
a
aa456
b123
bbb789
  • addr1,addr2: 從 addr1 開始,到 addr2 結束,包括首尾.
[ming@localhost test]$ sed -n '2,4p' data.txt
a123
aa456
aaa789

[ming@localhost test]$ sed -n '/123/,/123/p' data.txt
a123
aa456
aaa789
b123
  • addr,+n: 從 addr 開始(包括),直到往後 n 行.
[ming@localhost test]$ sed -n '2,+2p' data.txt
a123
aa456
aaa789

[ming@localhost test]$ sed -nr '/a{3}/,+2p' data.txt
aaa789
b123
bb456

1. 替換: s/regexp/replacement/

替換每行第一處匹配的地方:將 a 替換成 A

[ming@localhost test]$ sed 's/a/A/' data.txt
A
A123
Aa456
Aaa789
b123
bb456
bbb789

替換每行第 2 處匹配的地方:

[ming@localhost test]$ sed 's/a/A/2' data.txt
a
a123
aA456
aAa789
b123
bb456
bbb789

替換每行所有匹配的地方:g

[ming@localhost test]$ sed 's/a/A/g' data.txt
A
A123
AA456
AAA789
b123
bb456
bbb789

使用正則表達式:

[ming@localhost test]$ sed -r 's/[0-9]+/Number/' data.txt
a
aNumber
aaNumber
aaaNumber
bNumber
bbNumber
bbbNumber

引用匹配的內容:&

[ming@localhost test]$ sed -r 's/[0-9]+/<&>/' data.txt
a
a<123>
aa<456>
aaa<789>
b<123>
bb<456>
bbb<789>

引用匹配的分組:\n

[ming@localhost test]$ sed -r 's/a{1,3}([0-9]+)/<\1>/' data.txt
a
<123>
<456>
<789>
b123
bb456
bbb789

\n: 引用第 n 個分組.

2. 刪除行: d

[ming@localhost test]$ sed '/a\+/d' data.txt 
b123
bb456
bbb789

3. 插入行: i\a\

插到指定行之前:i\

[ming@localhost test]$ sed -r '/a+/i\line1\n\line2' data.txt 
line1
line2
a
line1
line2
a123
line1
line2
aa456
line1
line2
aaa789
b123
bb456
bbb789

插到指定行之後:a\

[ming@localhost test]$ sed -r '/a+/a\line1' data.txt 
a
line1
a123
line1
aa456
line1
aaa789
line1
b123
bb456
bbb789

4. 替換行: c\

[ming@localhost test]$ sed -r '/a+/c\line has a' data.txt 
line has a
line has a
line has a
line has a
b123
bb456
bbb789

5. 映射: y/source/dest/

[ming@localhost test]$ sed 'y/ab/AB/' data.txt 
A
A123
AA456
AAA789
B123
BB456
BBB789

6. 讀寫文件

將指定的行寫入指定的文件:w File

[ming@localhost test]$ sed -nr '/a+/w a.txt' data.txt 
[ming@localhost test]$ cat a.txt 
a
a123
aa456
aaa789

讀取文件內容,並將其插到指定行之後:r File

[ming@localhost test]$ cat input.txt 
----
[ming@localhost test]$ sed -r '/a+/r input.txt' data.txt 
a
----
a123
----
aa456
----
aaa789
----
b123
bb456
bbb789

7. 使用變量

[ming@localhost test]$ rpl="NUMBER"
[ming@localhost test]$ sed -r 's/[0-9]+/'"$rpl"'/' data.txt 
a
aNUMBER
aaNUMBER
aaaNUMBER
bNUMBER
bbNUMBER
bbbNUMBER

8. 使用腳本文件

[ming@localhost test]$ cat script.sed 
/a+/ {
    i\a begin
    a\a end
}

s/[0-9]+/Number/

[ming@localhost test]$ sed -rf script.sed data.txt 
a begin
a
a end
a begin
aNumber
a end
a begin
aaNumber
a end
a begin
aaaNumber
a end
bNumber
bbNumber
bbbNumber
user avatar philadelphia 頭像
1 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.