Shell 四劍客之 sed a i 新增新行

2021-10-06 05:45:45 字數 1091 閱讀 7929

[root@www ~]# cat sed.sh 

192.168.179.1.15

192.168.179.1.16

192.168.179.1.17

[root@www ~]# sed '/1.17/amytest' sed.sh --使用a然後再匹配到的1.17下面插入一行test

192.168.179.1.15

192.168.179.1.16

192.168.179.1.17

mytest

[root@www ~]# sed '/1.17/imytest' sed.sh --在其前一行插入使用i

192.168.179.1.15

192.168.179.1.16

mytest

192.168.179.1.17

[root@www ~]# sed '/1.17/i \ mytest' sed.sh --加上空格要使用轉義\

192.168.179.1.15

192.168.179.1.16

mytest

192.168.179.1.17

[root@www ~]# sed -e '/1.17/atest' -e '/1.17/imytest' sed.sh

192.168.179.1.15

192.168.179.1.16

mytest

192.168.179.1.17

test

在第二行後(亦即是加在第三行)加上『drink tea?』字樣!

[root@wwww~]# nl /etc/passwd | sed '2a drink tea'

1 root:x:0:0:root:/root:/bin/bash

2 bin:x:1:1:bin:/bin:/sbin/nologin

drink tea

3 daemon:x:2:2:daemon:/sbin:/sbin/nologin

.....(後面省略).....

那如果是要在第二行前

nl /etc/passwd | sed '2i drink tea'

shell四劍客之sed

格式 sed nefri 動作 其中n e f r i 動作有a,i,c,p,s n 預設把sed命令執行的結果,與原來的資料一起輸出到螢幕,如果加了 n,則只顯示sed處理後的內容 sed n 1p sed.txt a 在匹配的當前行的後面新增內容,add sed ahelloworld sed....

shell四劍客之find

find主要用來查詢檔案名,其基本格式 find path option print exec ok command 常用引數如下 name find name four 查詢名為four的檔案 包括目錄和檔案 find name fou 查詢開頭是fou的檔案 find name a z 查詢大寫...

shell四劍客之sed

sed是乙個非互動式文字編輯器,它一次處理一行內容。作用 編輯乙個或多個檔案,簡化對檔案的反覆操作 編寫轉換程式等 在處理文字時把當前處理的行儲存在臨時緩衝區中,稱為 模式空間 緊接著用sed命令處理緩衝區中的內容,處理完後把緩衝區的內容輸出至螢幕或寫入檔案。預設輸出到螢幕的文字已經修改,但檔案內容...