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

歡迎訪問 生活随笔!

生活随笔

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

linux

linux sed举例,sed 常用命令与参数,带举例:时时更新!

發布時間:2023/12/15 linux 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux sed举例,sed 常用命令与参数,带举例:时时更新! 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

格式

sed [-nefr] [n1,n2] 動作-n 安靜模

式,只有經過sed處理過的行才顯示出來,其他不顯示。-e 直接在命

令行模式上進行sed的操作。貌似是默認選項,不用寫。-f 將sed的操作寫在一個文

件里,用的時候

-f filename 就可以按照內容進行sed操作了。-r 使之支持擴展正則表達式n1,n2 不一定需要,

選擇要進行處理的行, 10,20 表示在10~20行之間處理動作a 添加,接字符串,添加到當前行的下一行。c 替換, 接字符串,用他們替換n1到n2之間的行。d 刪除符合模式的行

Example:sed '/regexp/d'?//

之間是正則表達式,模式在d前面,d后面一般不接任何內容。i 插入,接字符串,添加到當前行的上一行。p 打印,打印某個選擇的數

據,通常與-n 安靜模式一起使用s 搜索,

還可以替換,類似與vim里的搜索替換功能。Example:

sed '1,20s/old/new/g' 替換1~20行的old為new注意:動作最好用'

'括起來,防止空格導致錯誤。Example:顯示

passwd內容,將2~5行刪除顯示root@localhost:~/tmp$ cat -n /etc/passwd |sed '2,5d'

1?root:x:0:0:root:/root:/bin/bash6?games:x:5:60:games:/usr/games:/bin/sh7?man:x:6:12:man:/var/cache/man:/bin/sh

8?lp:x:7:7:lp:/var/spool/lpd:/bin/sh............在第二行后面一行加上Hello China 字符串root@localhost:~/tmp$ cat -n /etc/passwd |sed '2a Hello

China!'1?root:x:0:0:root:/root:/bin/bash2?daemon:x:1:1:daemon:/usr/sbin:/bin/shHello China!

......在第二行后面一行加上兩行字,例如

"this is first line!" "this is second line!"

root@localhost:~/tmp$ cat -n /etc/passwd |sed '2a This is first line!

\//使用續航符\后按回車輸入后續行> This is second line!'// 以' 再回車結束1?root:x:0:0:root:/root:/bin/bash

2?daemon:x:1:1:daemon:/usr/sbin:/bin/sh

This is first line!

This is second line!

3?bin:x:2:2:bin:/bin:/bin/sh將2~5行內容替換成

我是大好人!root@localhost:~/tmp$ cat -n /etc/passwd | sed '2,5c 我是大好人!'1?root:x:0:0:root:/root:/bin/bash

我是大好人!6?games:x:5:60:games:/usr/games:/bin/sh

7?man:x:6:12:man:/var/cache/man:/bin/sh

8?lp:x:7:7:lp:/var/spool/lpd:/bin/sh只顯示5~7行, 注意p 與-n 配合使用!root@localhost:~/tmp$ cat -n /etc/passwd |sed -n '5,7p'5?sync:x:4:65534:sync:/bin:/bin/sync

6?games:x:5:60:games:/usr/games:/bin/sh

7?man:x:6:12:man:/var/cache/man:/bin/sh備注:上述sed命令修改后只是顯示,并沒有寫入到文件中,如果想

寫入到文件中,sed 改變為sed

-i使用ifconfig

列出IP,

我們只想要

eth0的 IP地址可以這樣,先用grep

取出有IP的那一行,然后用sed去掉(替換成空) IP前面和后面的內容。root@localhost:~/tmp$ ifconfig

eth0

eth0?Link encap:Ethernet HWaddr

00:16:36:02:41:aa inet

addr:172.30.171.35 Bcast:172.30.171.255

Mask:255.255.255.0UP

BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX

packets:1221198 errors:0 dropped:0 overruns:0 frame:0

TX

packets:1125085 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX

bytes:1477365271 (1.3 GB) TX bytes:141539593 (134.9

MB)Interrupt:20

root@localhost:~/tmp$ ifconfig

eth0 | grep inet |sed 's/^.*addr://g' | sed 's/Bcast.*$//g'

172.30.171.35

root@localhost:~/tmp$

^.*addr: 表示

從開頭到addr:的字符串,s/^.*addr://g 表示將它替換為空,

Bcast.*$ 表示從Bcast到結尾的串,s/Bcast.*$//g 表示將它替換為空,//s 為搜索則剩下IP了!在/etc/manpath.config中,將有MAN的設

置取出,但不要說明內容。root@localhost:~/tmp$cat /etc/manpath.config |grep 'MAN'

|sed 's/#.*$//g' |sed '/^$/d'

MANDATORY_MANPATH?/usr/man

MANDATORY_MANPATH?/usr/share/man

MANDATORY_MANPATH?/usr/local/man

MANPATH_MAP?/bin?/usr/share/man

MANPATH_MAP?/usr/bin?/usr/share/man

MANPATH_MAP?/sbin?/usr/share/man

MANPATH_MAP?/usr/sbin?/usr/share/man

...........注意#不一定出現在行首。

因此

#.*$ 表示

#和后面的數據直到行尾,s/#.*$//g 即一行注釋,將他們替換成空^$

表示空行,后接d 表示刪除空行。注意:刪除空行不能用替換方法,因為空行替換成空后,還是有換行符在那一行。

總結

以上是生活随笔為你收集整理的linux sed举例,sed 常用命令与参数,带举例:时时更新!的全部內容,希望文章能夠幫你解決所遇到的問題。

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