shell指令碼之sed開發

2021-09-29 10:33:44 字數 3709 閱讀 1424

/tmp/

file

.txt

# my qq num is

49000448.$

not4900000448.

my god ,i am not oldbey,but clsn!$

#cheng

my name is cheng.$

not oldman.

my god ,i am not oldbey,but clsn!$

i like linux

2:在/tmp/

file

.txt檔案中不以#開頭的行的行首增加#號

sed -ri '/^#/!s/^/#/'

file

.txt

3:用命令列更改/tmp/

file

.txt檔案,把裡面所有的「name」更改為「address」

sed -i 's/name/address/g'

file

.txt

4:利用sed命令將/tmp/

file

.txt中所有的回車替換成空格?

sed -r ':jix;n;s/\n/ /g;b jix'

file

.txt

5:為/tmp/

file

.txt檔案中前2行的行首加#號

sed -ir '1,2s/^/#/'

file

.txt

6:列印/tmp/

file

.txt檔案中的第5行

[root@chengyinwu tmp]

# sed -n '5p' file.txt

7:刪除/tmp/

file

.txt檔案中的帶特殊字元的行

sed -ri '/(^[0-9]|^[a-z])/!d'

file

.txt

8:刪除#號及#後面的所有空白字元;

sed -ri 's/^#[[:space:]]+//g'

file

.txt

sed -ri 's/#//g'

file

.txt

9:查詢/tmp/

file

.txt檔案中1到10行之間,同時將"qq"替換為"we"

,"not"替換"no"

-e '1,10s/not/no/g'

file

.txt

10:使用sed命令列印出/tmp/

file

.txt檔案的第一行到第三行

[root@chengyinwu tmp]

# sed -n '1,3p' file.txt

11:刪除檔案/tmp/

file

.txt中所有帶有數字的行

sed -i '/[0-9]/d'

file

.txt

12:刪除/tmp/

file

.txt檔案第3行到第10行的內容?

sed -i '3,10d'

file

.txt

13:刪除/tmp/

file

.txt檔案中的行首的空白字元。

sed -ri 's/^ +//'

file

.txt

14:使用sed將/tmp/

file

.txt檔案中第2行的448替換成558

[root@chengyinwu tmp]

# sed -ri '2s#448#558#' file.txt

15:使用sed將/tmp/

file

.txt檔案中所有$刪除

16:將/tmp/

file

.txt中所有小寫字母替換成大寫字母

sed -ri 's/[a-z]/\u&/g'

file

.txt

17:將/tmp/

file

.txt檔案中第2到第8行之間所有大寫字母替換成小寫字母

sed -i '2,8s/[a-z]/\l&/g'

file

.txt

18:使用sed找出/tmp/

file

.txt檔案中包含cheng的行

[root@chengyinwu tmp]

# sed -n '/cheng/p' file.txt

19:將/tmp/

file

.txt檔案中以;結尾的行,行首插入#

20:將/tmp/

file

.txt檔案中第3和第5行的大寫字母替換成小寫字母

sed -i '3s/[a-z]/\u&/g;5s/[a-z]/\u&/g'

file

.txt

21:刪除/tmp/

file

.txt檔案中第2行到下乙個以#號開頭的行之間所有空行

sed -ir '2,/^#/'

file

.txt

22:刪除file

.txt檔案中的空行

sed -i '/^$/d'

file

.txt

23:刪除/tmp/

file

.txt檔案中所有以#開頭的行的行首的#

sed -i '/^#/s/#//'

file

.txt

24:使用sed將selinux徹底關閉

[root@chengyinwu tmp]

# sed -i '/^selinux=/c selinux=disabled' /etc/selinux/config

25:修改passwd檔案中第4行到第7行中所有的/sbin/nologin為/bin/bash

[root@chengyinwu tmp]

# sed -ri '4,7s#\/sbin\/nologin#\/bin\/bash#gp' passwd

26:把/目錄下所有以.txt結尾的檔案中包含yinwul的字串全部替換為cheng

sed -i 's/yinwul/cheng/g' $(find ./-

type f -name "*.txt"

) find ./-

type f -name "*.txt"

|xargs sed -i 's/cheng/yinwul/g'

27:passwd檔案的第2、8行前面都追加 "insert line before"

[root@chengyinwu tmp]

# sed -i '2,8 i insert line before' passwd

28:將passwd檔案的內容,新增到/tmp/

file

.txt檔案中第3行的後面

[root@chengyinwu tmp]

# sed -i '3r passwd' /tmp/file.txt

29:使用sed命令列印出系統版本

hostnamectl |sed -nr '/operating system/s#(^.*x) (.*) (\(co.*)#\2#gp'

[root@chengyinwu tmp]

# ifconfig eth0 |sed -nr 's#^.*inet (.*) (.*)net.*$#\1#gp'

31:把data目錄及其子目錄下所有以副檔名.txt結尾的檔案中包含yinwul的字串全部替換為cheng.

Shell指令碼學習之sed詳解

linux 1 目錄 sed編輯器逐行處理檔案 或輸入 並將結果傳送到螢幕。具體過程如下 首先sed把當前正在處理的行儲存在乙個臨時快取區中 也稱為模式空間 然後處理臨時緩衝區中的行,完成後把該行傳送到螢幕上。sed每處理完一行就將其從臨時緩衝區刪除,然後將下一行讀入,進行處理和顯示。處理完輸入檔案...

shell指令碼工具之sed命令

sed就是批處理的流編輯器,可以對來自檔案或標準輸入的輸入流進行轉換,sed通常被用作管道中的過濾器.由於sed僅僅對其輸入進行一遍掃瞄,因此比其它互動式編輯器更加高效.檔案內容 root tong1 opt cat sed.txt 1tong 2 cheng 3 hellow 4word wu h...

shell指令碼之sed工具使用

執行 顯示 sed 選項 操作 引數 sed 選項 f 指令碼檔案 引數 3.3.1 p 輸出符合條件的文字 root localhost sed n p test.txt 輸出所有內容,等同於 cat test.txt 省略內容 root localhost sed n 3p test.txt 輸...