shell程式設計四劍客之SED

2021-10-08 13:35:41 字數 3980 閱讀 3326

sed

[-options]

[『commands』] filename;

sed工具預設處理文字,文字內容輸出螢幕已經修改,但是檔案內容其實沒有修改,需要加-i引數即對檔案徹底修改;

x #x為指定行號;

x,y #指定從x到y的行號範圍;

/pattern/ #查詢包含模式的行;

/pattern/pattern/ #查詢包含兩個模式的行;

/pattern/,x #從與pattern的匹配行到x號行之間的行;

x,/pattern/ #從x號行到與pattern的匹配行之間的行;

x,y!

#查詢不包括x和y行號的行;

r #從另乙個檔案中讀檔案;

w #將文字寫入到乙個檔案;

y #變換字元;

q #第乙個模式匹配完成後退出;

l #顯示與八進位制ascii碼等價的控制字元;

#在定位行執行的命令組;

p #列印匹配行;

=#列印檔案行號;

a\ #在定位行號之後追加文字資訊;

i\ #在定位行號之前插入文字資訊;

d #刪除定位行;

c\ #用新文字替換定位文字;

s #使用替換模式替換相應模式;

n #讀取下乙個輸入行,用下乙個命令處理新的行;

n #將當前讀入行的下一行讀取到當前的模式空間。

[root@localhost ~]

# cat test.txt

192.168.2.1 root 123456

192.168.2.2 root 123456

192.168.2.3 root 123456

192.168.2.4 admin 123456

[root@localhost ~]

# sed 's/root/admin/g' test.txt

192.168.2.1 admin 123456

192.168.2.2 admin 123456

192.168.2.3 admin 123456

192.168.2.4 admin 123456

#從這裡可以看得出來上面的命令只是把你的操作放在臨時緩衝區了

[root@localhost ~]

# cat test.txt

192.168.2.1 root 123456

192.168.2.2 root 123456

192.168.2.3 root 123456

192.168.2.4 admin 123456

#這裡加個-i指令就可以了

[root@localhost ~]

# sed -i 's/root/admin/g' test.txt

[root@localhost ~]

# cat test.txt

192.168.2.1 admin 123456

192.168.2.2 admin 123456

192.168.2.3 admin 123456

192.168.2.4 admin 123456

加了-i引數才可以把內容真正的修改到文字檔案中,不加引數可以作為測試用

[root@localhost ~]

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

192.168.2.1 admin 123456

192.168.2.2 admin 123456

192.168.2.3 admin 123456

或者:[root@localhost ~]

# head -3 test.txt

192.168.2.1 admin 123456

192.168.2.2 admin 123456

192.168.2.3 admin 123456

[root@localhost ~]

# sed -n '1p;$p' test.txt

192.168.2.1 admin 123456

192.168.2.4 admin 123456

或者:[root@localhost ~]

# head -1 test.txt && tail -1 test.txt

192.168.2.1 admin 123456

192.168.2.4 admin 123456

[root@localhost ~]

# sed -i '1,3d' test.txt

[root@localhost ~]

# sed -i '/admin/,$d' test.txt

[root@localhost ~]

# for i in `seq 1 6`;do sed -i '$d' test.txt;done

[root@localhost ~]

# sed -i '$d' test.txt

[root@localhost ~]

# sed -i '/192.168.2.3/ahello word' test.txt

[root@localhost ~]

# sed -i '/192.168.2.3/ihello word' test.txt

[root@localhost ~]

# sed -i 's/^192.168.2.3/& hello word/g'' test.txt

[root@localhost ~]

# sed -i 's/123456$/& word/g' test.txt

[root@localhost ~]

# sed -i '/admin/s/^/&word /g' test.txt

[root@localhost ~]

# sed -i -e '/root/s/^/&1./' -e '/root/s/$/&./' test.txt

[root@localhost ~]

# sed -i '/root/s/^/&1./;/root/s/$/&./' test.txt

user=admin

[root@localhost ~]

# sed -i "s/root/$user/g" test.txt

[root@localhost ~]

# sed -i "/selinux/s/enforcing/disabled/" /etc/selinux/config

[root@localhost ~]

# sed 'n;s/\n/ /' test.txt

[root@localhost ~]

# sed -i '2s/admin/root/g test.txt

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四劍客之sed

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

Shell程式設計四劍客之AWK

awk是乙個優良的文字處理工具,linux 及unix 環境中現有的功能最強大的資料處理引擎之一,以aho weinberger kernighan三位發明者名字首字母命名為awk,awk是乙個行級文字高效處理工具,awk經過改進生成的新的版本有nawk gawk,一般linux預設為gawk,ga...