[shell基础]——sed命令
---恢復(fù)內(nèi)容開始---
關(guān)于sed
sed 是一種在線編輯器,它一次處理一行內(nèi)容。
處理時,把當(dāng)前處理的行存儲在臨時緩沖區(qū)中,稱為“模式空間”(pattern space),接著用sed命令處理緩沖區(qū)中的內(nèi)容,處理完成后,把緩沖區(qū)的內(nèi)容送往屏幕。接著處理下一行,這樣不斷重復(fù),直到文件末尾。文件內(nèi)容并沒有 改變,除非你使用重定向存儲輸出。
sed主要用來自動編輯一個或多個文件;簡化對文件的反復(fù)操作;編寫轉(zhuǎn)換程序等。
?
sed詳解
sed? [選項]? sed編輯命令? 輸入文件
shell 命令 |? sed? [選項] ? sed編輯命令
sed? [選項]? -f? sed腳本文件? 輸入文件
常見的sed選項
-n? 只顯示匹配處理的行
-i??? 直接在文件中修改而不顯示
-r?? 支持擴展正則表達式
-e? 執(zhí)行多個編輯命令時(一般用;代替)
常見的sed編輯命令
p:打印匹配行 print
d:刪除指定行 delete
a:在匹配行后面追加 append
i:在匹配行前面插入 insert
c:整行替換
r:將文件的內(nèi)容讀入 read
w:將文本寫入文件 write
s:字符串替換(匹配正則表達式)substitution
?
(1) '/? /p' = !
顯示x到y(tǒng)行
# sed -n 'x,yp'
x到y(tǒng)行不打印
# sed -n 'x,y!p' !取反
顯示x到x+y行
# sed -n 'x,+yp'
顯示第1行
# sed -n '1p'
顯示最后1行
# sed -n '$p'
顯示以0-9數(shù)字開頭的行
# sed -n '/^[0-9]/p'
# sed -n '/正則表達式/{x,yp}'
輸出匹配行并顯示其在文本中的行號
# sed -n '/正則表達式/{=;p}'
?
(2) '/ /d'
# sed -n 'x,yd'
# sed -n '/正則表達式/{x,yd}'
刪除所有空白行
# sed '/^$/d'
?
(3)? ; 或 -e
刪除空行和#號開頭的行
# sed '/^$/d ; /^#/d'
# sed -e '/^$/d' -e '/^#/d'?? 多條件刪除是用';'
(4) '/? /a\ ' 行后追加 ? ? ? '/? /a' 下一行追加
把三個***號加到Fred開頭的行后面
# cat gongdatext.txt | sed '/^Fred/a\***'
在第n行的下一行追加一行內(nèi)容(注意n不能是文本不存在的行數(shù))
# seq 'na 111111111'
在匹配的行的下一行追加一行內(nèi)容(注意是另起一行追加)
# seq '/正則表達式/a 111111111'
(5) '/ ? /i' 行前追加
在第n行的上一行追加一行內(nèi)容
# seq 'ni 111111111'
在最后一行的上一行追加一行內(nèi)容
# seq '$i 111111111'
在匹配的行的上一行追加一行內(nèi)容
# seq '/正則表達式/i 111111111'
(6) '/? /c' 整行替換
將第n行整行的內(nèi)容替換為該內(nèi)容
# seq 'nc 111111111'
將匹配行內(nèi)容替換為該內(nèi)容
# seq '/正則表達式/c 111111111'
(7) 's/old/new/g' 替換
同時將# @替換成gongda
# cat gongda.txt | sed -e 's/#/gongda/g' -e 's/@/gongda/g'
同時將1,2行的abcdefg替換alvin
# cat gongda.txt | sed -n '1,2s/abcdefg/alvin/p'
(8) -i 將修改寫入原文件
(9) r
在test.txt文件的末尾后面讀入/etc/hosts文件的內(nèi)容
# sed '$r /etc/hosts' test.txt
在/dev/sda1后面讀入/etc/mtab文件的內(nèi)容
# df -h | sed '/dev\/sda1/r /etc/mtab'
?
sed練習(xí)題
測試文本為gongdatext.txt,內(nèi)容如下:
???? 1?? ?Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300
???? 2?? ?Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
???? 3?? ?Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400
???? 4?? ?Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
???? 5?? ?Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900
???? 6?? ?Jon's DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
???? 7?? ?Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100
???? 8?? ?Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200
???? 9?? ?Karen Evich:284-758-2867:23 November-December Edgecliff Place, Lincoln, NB 92743:11/3/35:58200
??? 10?? ?"Fred" Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900
??? 11?? ?Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900
??? 12?? ?Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
??? 13?? ?Paco Gutierrez:835-365-1284:454 Easy Street, Decatur, IL 75732:2/28/53:123500
??? 14?? ?Ephram Hardy:293-259-5395:235 CarltonLane, Joliet, IL 73858:8/12/20:56700
??? 15?? ?James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
??? 16?? ?Barbara Kertz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500
??? 17?? ?Lesley Kirstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
??? 18?? ?William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
??? 19?? ?Sir Lancelot:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500
??? 20?? ?Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000
??? 21?? ?Zippy Pinhead:834-823-8319:2356 Bizarro Ave., Farmount, IL 84357:1/1/67:89500
??? 22?? ?Arthur Putie:923-835-8745:23 Wimp Lane, Kensington, DL 38758:8/31/69:126000
??? 23?? ?Popeye Sailor:156-454-3322:945 Bluto Street, Anywhere, USA 29358:3/19/35:22350
??? 24?? ?Jose Santiago:385-898-8357:38 Fife Way, Abilene, TX 39673:1/5/58:95600
??? 25?? ?Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200
??? 26?? ?Yukio Takeshida:387-827-1095:13 Uno Lane, Ashville, NC 23556:7/1/29:57000
??? 27?? ?Vinh Tranh:438-910-7449:8235 Maple Street, Wilmington, VM 29085:9/23/63:68900
(1)把Jon's 臨時替換成Jonathan.
# sed "s/Jon's/Jonathan/g" gongdatext.txt
(2)把"Fred" 全部替換成gongda,其中包括了""號一起替換
# sed 's/"Fred"/gongda/g' gongdatext.txt
(3)刪除頭三行和第11行
# cat gongdatext.txt | sed? -e '1,3d' -e '11d'
(4)顯示5-10行
# sed -n '5,10p' gongdatext.txt
(5)顯示關(guān)鍵字November-December的行號
# sed -n -e '/November-December/=' gongdatext.txt
9
(6)把三個***號加到Fred開頭的行后面
# sed -n -e '/^Fred/p' -e '/^Fred/a\***' gongdatext.txt
(7)過濾包含Jose的行,然后用He Xi Gong 'da' 替換Jose.
# sed -n? "s/Jose/He Xi gong 'da'/p"? gongdatext.txt
Jon's DeLoach:408-253-3122:123 Park St., San He Xi gong 'da', CA 04086:7/25/53:85100
He Xi gong 'da' Santiago:385-898-8357:38 Fife Way, Abilene, TX 39673:1/5/58:95600
(8)把Popeye的生日改為11/14/46
# sed '23s/3\/19\/35/11\/14\/46/g' gongdatext.txt
(9)刪除所有空白行
# sed? '/^$/d' gongdatext.txt
(10)將里面包含關(guān)鍵字/全部替換成-
# sed 's/\//-/g' gongdatext.txt
(11)請將電話號碼如:238-923-8366 中的- 替換為空
# sed 's/-/ /g' gongdatext.txt
轉(zhuǎn)載于:https://www.cnblogs.com/snsdzjlz320/p/5689082.html
總結(jié)
以上是生活随笔為你收集整理的[shell基础]——sed命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++设计模式-Composite组合模
- 下一篇: 【README】二叉树刷题框架