日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux中sed的基本用法,linux sed用法

發布時間:2025/3/20 linux 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux中sed的基本用法,linux sed用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、定義

sed

流編輯器,每次從輸入中讀取一行,用提供的編輯器命令匹配數據、按命令中指定的方式修改流中的數據,然后將生成的數據輸出到STDOUT,在流編輯器將所

有命令與一行數據進行匹配后,它會讀取下一行數據并重復這個過程,在流編輯器處理完流中的所有數據后,它就會終止。

二、格式

sed options script file

三、options選項

編號

option

解釋

1

-n

不要為每隔命令生成輸出,等待print命令來輸出

2

-e

script 可以運行多個命令

3

-f

file 在處理時,將file中指定的命令添加到運行的命令中

4

-i

將修改后的結果寫到源文件

新建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 選項,則不會打印輸出結果,只有加上 p 選項,才可以輸出結果

示例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

可以運行多個命令

示例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]# 注意:兩個命令之間必須用分號分割1

2

3

4

5

3. -f

新建一個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 :為可選命令,可以表示執行的地方,如 1 只操作第一行, 1-3 操作 1-3 行, /san/ 只操作含有字符串”san”的那一行, /san/,/wu/ 操作從字符串”san” 到“wu”的行

A :需要被替換的字符

B :替換后的字符

C :可選命令,一些標志

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

注意:這里只是替換了第一行中的第一個 ‘s’,若想替換第一行中的全部 ‘3’,則需要使用可選標志 g ,再下面會介紹,這里先展示一下

[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”

此時,不需要前面的可選命令[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)可選標志

標號

可選標志

解釋

1

數字

表明新文本要替換第幾處模式匹配的地方

2

g

表明新文本將會替換所有已有文本出現的地方

3

p

表明要將原來行的內容打印出來

4

w

file 將替換后的結果寫到file中去

5

i

忽略大小寫

1.數字

在上面基本操作中,我們使用命令 sed ‘1 s/s/666/’ test3.txt 表示將第一行中的第一個 ‘s’替換為”666”,可以和這個對比一下

[root@localhost shell]# sed'1 s/s/666/2' test3.txt 101,Zhang san,Bo666s //替換第二個’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 //替換第三個’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 一起使用,只會打印出修改過的行

[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 選項只是將修改過的行輸出到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中的唯一命令,還有一些其它的命令

編號

其它命令

解釋

1

d

刪除

2

i

在指定行前增加一個新行

3

a

在指定行后增加一個新行

4

c

修改某一行的內容

5

y

轉換命令

6

r

從文件中讀取數據

新建一個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

在指定行的前面添加一個新行

示例1:

在第2行前面添加一個新行

[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”前面插入一個新行

[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

在指定行的后面添加一個新行

示例1:

在第2行后面添加一個新行

[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”后面插入一個新行

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

其作用是替換某一行中的數據

示例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

轉換命令是唯一可以處理單個字符的sed編輯器命令,格式如下:

y/A/B/

其功能是,A中的第一個字符被替換為B 中的第一個字符,A中的第二個字符被替換為B 中的第二個字符,A中的第三個字符被替換為B 中的第三個字符,直到A中的字符和B中額字符被一一對應完畢,也就是是說A中的字符長度要和B中的字符長度一致,若不一致,則會報錯。

示例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

讀取數據

示例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中的數據 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中的數據 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

總結

以上是生活随笔為你收集整理的linux中sed的基本用法,linux sed用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 天天躁夜夜躁av天天爽 | 白石茉莉奈中文字幕在 | 成人h网站| 亚洲色图美腿丝袜 | 日本女v片 | 一级在线播放 | 超碰在线观看免费版 | 欧美日韩精品一二三区 | 国产一区亚洲二区三区 | 成人午夜网 | 亚洲一区 在线播放 | 亚洲国产中文字幕在线观看 | 精品一区二区在线免费观看 | 亚洲国产图片 | 天天干夜操 | 欧美日韩视频一区二区三区 | 日日干日日草 | www.九九热.com | 一区二区三区xxx | 蜜臀va| 欧美刺激性大交 | 亚洲激情在线播放 | 国产视频手机在线观看 | 欧美无砖专区免费 | 日韩av无码一区二区三区 | 欧美大片网站 | 国产伦精品一区二区三区四区 | 天天色天天 | 国产区在线视频 | 激情婷婷六月 | 亚洲男人天堂2023 | 快播久久 | 久久精品国产亚洲av麻豆图片 | 交专区videossex非洲 | 日毛片| 成人高清在线 | 成年人黄国产 | 成人动漫在线观看视频 | 中文字幕h| 日韩毛片免费看 | 国产精品theporn88 | 手机在线看片日韩 | 日本a级黄 | 欧美色图亚洲视频 | 国内成人免费视频 | 欧美激情性做爰免费视频 | 毛片最新网址 | 成年女人色毛片 | 黄色视屏在线看 | 四虎av在线播放 | 色玖玖| 美女的奶胸大爽爽大片 | 99无码熟妇丰满人妻啪啪 | wwwxxxx在线观看 | 成人毛片在线精品国产 | 7777精品久久久久久 | a级一片 | 午夜视频福利 | 亚洲精品久久久中文字幕 | 天堂网中文字幕 | 泰剧19禁啪啪无遮挡 | 乱色欧美 | 国产精品丝袜视频 | 国产在成人精品线拍偷自揄拍 | 初音未来打屁股 | 成人福利免费视频 | 妺妺窝人体色www在线下载 | 国产精品久久久久久无人区 | 国产在线视频网 | 黄网视频在线观看 | 精品乱子伦一区二区三区 | 我把护士日出水了视频90分钟 | 日日骚网| 亚洲网站一区 | 日本美女视频网站 | 色视屏| 91在线免费视频观看 | 欧美成人性色 | 在线不卡二区 | youjizz.com国产| 黄页网站在线看 | 成人一级毛片 | 毛片精品 | 高清视频在线免费观看 | 国产精品s | 国产成人免费在线 | 99re99| 国产专区在线 | 一级片视频网站 | 福利在线看 | 韩国伦理电影免费在线 | 国产福利91精品一区二区三区 | www.插插插.com| 日本精品一区二区三区在线观看 | 日本久久成人 | 成人福利小视频 | 91av视频在线播放 | 日本美女日批视频 | 香蕉在线视频播放 |