linux中sed的基本用法,linux sed用法
一、定義
sed
流編輯器,每次從輸入中讀取一行,用提供的編輯器命令匹配數(shù)據(jù)、按命令中指定的方式修改流中的數(shù)據(jù),然后將生成的數(shù)據(jù)輸出到STDOUT,在流編輯器將所
有命令與一行數(shù)據(jù)進(jìn)行匹配后,它會(huì)讀取下一行數(shù)據(jù)并重復(fù)這個(gè)過程,在流編輯器處理完流中的所有數(shù)據(jù)后,它就會(huì)終止。
二、格式
sed options script file
三、options選項(xiàng)
編號(hào)
option
解釋
1
-n
不要為每隔命令生成輸出,等待print命令來輸出
2
-e
script 可以運(yùn)行多個(gè)命令
3
-f
file 在處理時(shí),將file中指定的命令添加到運(yùn)行的命令中
4
-i
將修改后的結(jié)果寫到源文件
新建test3.txt
[root@localhost shell]# cat test3.txt 101,Zhang san,Boss 102,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
6
7
1. -n
若使用 -n 選項(xiàng),則不會(huì)打印輸出結(jié)果,只有加上 p 選項(xiàng),才可以輸出結(jié)果
示例1:
打印第一行
[root@localhost shell]# sed -n '1 p' test3.txt 101,Zhang san,Boss [root@localhost shell]#1
2
3
示例2:
打印1-3行
[root@localhost shell]# sed -n '1,3 p' test3.txt 101,Zhang san,Boss 102,Li si ,Manager 103,Wang wu,Programmer [root@localhost shell]#1
2
3
4
5
示例3:
打印含有字符串”san”的那一行
[root@localhost shell]# sed -n '/san/ p' test3.txt 101,Zhang san,Boss [root@localhost shell]#1
2
3
示例4:
打印含有字符串”san” 到字符串“wu”的所有行
[root@localhost shell]# sed -n '/san/,/wu/ p' test3.txt 101,Zhang san,Boss 102,Li si ,Manager 103,Wang wu,Programmer [root@localhost shell]#1
2
3
4
5
示例5:
每隔一行,打印一次
[root@localhost shell]# sed -n '1~2 p' test3.txt 101,Zhang san,Boss 103,Wang wu,Programmer 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
2. -e
可以運(yùn)行多個(gè)命令
示例1:
打印第1行和第2行
[root@localhost shell]# sed -n -e '1 p' -e '2 p' test3.txt 101,Zhang san,Boss 102,Li si ,Manager [root@localhost shell]#1
2
3
4
示例2:
打印第1行和第2行
[root@localhost shell]# sed -n -e '1 p ; 2 p' test3.txt 101,Zhang san,Boss 102,Li si ,Manager [root@localhost shell]# 注意:兩個(gè)命令之間必須用分號(hào)分割1
2
3
4
5
3. -f
新建一個(gè)test4.txt
[root@localhost shell]# cat test4.txt 1 p 2 p
[root@localhost shell]#1
2
3
4
打印第一行和第二行
[root@localhost shell]# sed -n -f test4.txt test3.txt 101,Zhang san,Boss 102,Li si ,Manager [root@localhost shell]#1
2
3
4
四、替換命令
其格式為:
sed ‘[D] s/A/B/[C]’
D :為可選命令,可以表示執(zhí)行的地方,如 1 只操作第一行, 1-3 操作 1-3 行, /san/ 只操作含有字符串”san”的那一行, /san/,/wu/ 操作從字符串”san” 到“wu”的行
A :需要被替換的字符
B :替換后的字符
C :可選命令,一些標(biāo)志
1)基本操作
1.把第一行中的 ‘s’ 替換為 “666”
[root@localhost shell]# sed '1 s/s/666/' test3.txt 101,Zhang 666an,Boss 102,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
6
7
注意:這里只是替換了第一行中的第一個(gè) ‘s’,若想替換第一行中的全部 ‘3’,則需要使用可選標(biāo)志 g ,再下面會(huì)介紹,這里先展示一下
[root@localhost shell]# sed '1 s/s/666/g' test3.txt 101,Zhang 666an,Bo666666 102,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
6
7
2.把1-3 行中的 ‘s’ 替換為 “666”
[root@localhost shell]# sed '1,3 s/s/666/' test3.txt 101,Zhang 666an,Boss 102,Li 666i ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
6
7
3.把含有字符”Li”那一行,中的‘s’ 替換為 “666”
[root@localhost shell]# sed '/Li/ s/s/666/' test3.txt 101,Zhang san,Boss 102,Li 666i ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
6
7
4.把每一行中的‘s’ 替換為 “666”
此時(shí),不需要前面的可選命令[D]即可
[root@localhost shell]# sed ' s/s/666/' test3.txt 101,Zhang 666an,Boss 102,Li 666i ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Cu666tomer service
[root@localhost shell]#1
2
3
4
5
6
7
8
2)可選標(biāo)志
標(biāo)號(hào)
可選標(biāo)志
解釋
1
數(shù)字
表明新文本要替換第幾處模式匹配的地方
2
g
表明新文本將會(huì)替換所有已有文本出現(xiàn)的地方
3
p
表明要將原來行的內(nèi)容打印出來
4
w
file 將替換后的結(jié)果寫到file中去
5
i
忽略大小寫
1.數(shù)字
在上面基本操作中,我們使用命令 sed ‘1 s/s/666/’ test3.txt 表示將第一行中的第一個(gè) ‘s’替換為”666”,可以和這個(gè)對(duì)比一下
[root@localhost shell]# sed'1 s/s/666/2' test3.txt 101,Zhang san,Bo666s //替換第二個(gè)’s’ 102,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service1
2
3
4
5
6
[root@localhost shell]# sed'1 s/s/666/3' test3.txt 101,Zhang san,Bos666 //替換第三個(gè)’s’ 102,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
6
7
2.g
[root@localhost shell]# sed'1 s/s/666/g' test3.txt 101,Zhang 666an,Bo666666 //將第一行中的全部’s’ 替換為 “666” 102,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service1
2
3
4
5
6
[root@localhost shell]# sed's/s/666/g' test3.txt 101,Zhang 666an,Bo666666 //將此文本中的所有’s’ 替換為 “666” 102,Li 666i ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Cu666tomer 666ervice
[root@localhost shell]#1
2
3
4
5
6
7
3.p
和-n 一起使用,只會(huì)打印出修改過的行
[root@localhost shell]# sed -n 's/s/666/gp' test3.txt 101,Zhang 666an,Bo666666 102,Li 666i ,Manager 105,Sun qi,Cu666tomer 666ervice
[root@localhost shell]#1
2
3
4
5
4.w file
w file 選項(xiàng)只是將修改過的行輸出到file中去
[root@localhost shell]# sed -n 's/s/666/gpw out.txt' test3.txt 101,Zhang 666an,Bo666666 102,Li 666i ,Manager 105,Sun qi,Cu666tomer 666ervice
[root@localhost shell]# cat out.txt 101,Zhang 666an,Bo666666 102,Li 666i ,Manager 105,Sun qi,Cu666tomer 666ervice
[root@localhost shell]#1
2
3
4
5
6
7
8
9
五、其它命令
文本替換命令并不是sed中的唯一命令,還有一些其它的命令
編號(hào)
其它命令
解釋
1
d
刪除
2
i
在指定行前增加一個(gè)新行
3
a
在指定行后增加一個(gè)新行
4
c
修改某一行的內(nèi)容
5
y
轉(zhuǎn)換命令
6
r
從文件中讀取數(shù)據(jù)
新建一個(gè)test3.txt
[root@localhost shell]# cat test3.txt 101,Zhang san,Boss 102,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
6
7
1. d
刪除命令
示例1:
刪除第一行
[root@localhost shell]# sed '1d' test3.txt 102,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
6
示例2:
刪除第一道第二行
[root@localhost shell]# sed '1,2d' test3.txt 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
示例3:
刪除含有字符”wu”的那一行
[root@localhost shell]# sed '/wu/d' test3.txt 101,Zhang san,Boss 102,Li si ,Manager 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
6
2. i
在指定行的前面添加一個(gè)新行
示例1:
在第2行前面添加一個(gè)新行
[root@localhost shell]# sed '2 i\666' test3.txt 101,Zhang san,Boss 666 102,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
6
7
8
示例2:
在字符”wu”前面插入一個(gè)新行
[root@localhost shell]# sed '/wu/ i\666' test3.txt 101,Zhang san,Boss 102,Li si ,Manager 666 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
6
7
8
3. a
在指定行的后面添加一個(gè)新行
示例1:
在第2行后面添加一個(gè)新行
[root@localhost shell]# sed '2 a\666' test3.txt 101,Zhang san,Boss 102,Li si ,Manager 666 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
6
7
8
示例2:
在字符”wu”后面插入一個(gè)新行
ot@localhost shell]# sed '/wu/ a\666' test3.txt 101,Zhang san,Boss 102,Li si ,Manager 103,Wang wu,Programmer 666 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
6
7
8
4.c
其作用是替換某一行中的數(shù)據(jù)
示例1:
將第二行替換為 “666”
[root@localhost shell]# sed '2c 666' test3.txt 101,Zhang san,Boss 666 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
6
7
示例2:
將含有字符串”wu”的那一行替換為 “666”
[root@localhost shell]# sed '/wu/c 666' test3.txt 101,Zhang san,Boss 102,Li si ,Manager 666 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
6
7
5.y
轉(zhuǎn)換命令是唯一可以處理單個(gè)字符的sed編輯器命令,格式如下:
y/A/B/
其功能是,A中的第一個(gè)字符被替換為B 中的第一個(gè)字符,A中的第二個(gè)字符被替換為B 中的第二個(gè)字符,A中的第三個(gè)字符被替換為B 中的第三個(gè)字符,直到A中的字符和B中額字符被一一對(duì)應(yīng)完畢,也就是是說A中的字符長度要和B中的字符長度一致,若不一致,則會(huì)報(bào)錯(cuò)。
示例1:
將1替換為 7 2替換為 8 3 替換為9
[root@localhost shell]# sed 'y/123/789/' test3.txt 707,Zhang san,Boss 708,Li si ,Manager 709,Wang wu,Programmer 704,Zhao liu,HR 705,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
6
7
示例2:
將第二行中的1替換為 7 2替換為 8 3 替換為9
[root@localhost shell]# sed '2 y/123/789/' test3.txt 101,Zhang san,Boss 708,Li si ,Manager 103,Wang wu,Programmer 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
6
7
6.r
讀取數(shù)據(jù)
示例1:
[root@localhost shell]# cat test5.txt 999 888 777 [root@localhost shell]# sed'3r test5.txt' test3.txt 101,Zhang san,Boss //在第三行后讀入test5.txt中的數(shù)據(jù) 102,Li si ,Manager 103,Wang wu,Programmer 999 888 777 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]# sed'1,3r test5.txt' test3.txt 101,Zhang san,Boss //在第一道第三行,讀取test5.txt中的數(shù)據(jù) 999 888 777 102,Li si ,Manager 999 888 777 103,Wang wu,Programmer 999 888 777 104,Zhao liu,HR 105,Sun qi,Customer service
[root@localhost shell]#1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
六、其它用法
1).替換字符
下面例子
#sed ‘s/\/bin\/bash/\/bin\/csh/’ /etc/passwd1
其目的是用C shell 替換/etc/passwd文件中的bash shell ,但是采用上面的方式看著比較困惑,因此可以采用替換字符的方式,如下:
sed ‘s!!!’ file #sed ‘s!/bin/bash!/bin/csh!’ /etc/passwd1
2
sed ‘@@@’ file #sed ‘s@/bin/bash@/bin/csh@’ /etc/passwd
總結(jié)
以上是生活随笔為你收集整理的linux中sed的基本用法,linux sed用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux crontab 管理,lin
- 下一篇: win10下安装linux mint,w