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

歡迎訪問 生活随笔!

生活随笔

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

linux

Linux sed 写命令常见使用案例

發(fā)布時間:2024/9/27 linux 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux sed 写命令常见使用案例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

? ? ? ? ? ? ? ? ? ? ? ? ? ?Linux sed w命令常見使用場景匯總

聲明與簡介

sed:Stream Editor文本流編輯,sed是一個“非交互式的”面向字符流的編輯器。本文主要以實際的數(shù)據(jù)來介紹sed的write space(寫空間)即w命令的使用。

數(shù)據(jù)說明

如下是使用的示例文本數(shù)據(jù),這里的行號是為了方便解釋結(jié)果,不是文本內(nèi)容。

1 7369,smith,clerk,7902,'1980-12-17',800,null,20 2 7499,allen,salesman,7698,'1981-2-20',1600,300,30 3 7521,ward,salesman,7698,'1981-2-22',1250,500,30 4 7566,jones,manager,7839,'1981-4-2',2975,null,20 5 7654,martin,salesman,7698,'1981-9-28',1250,1400,30 6 7655,jack,manager,7698,'1987-3-28',1600,1800,10 7 7656,tim,clerk,7902,'1982-12-12',1400,1400,30 8 7657,kate,clerk,7902,'1989-11-11',1400,1800,10 9 7698,blake,manager,7839,'1981-5-1',2850,null,30 10 7699,dlake,salesman,7839,'1983-6-15',3000,null,10 11 7782,clark,manager,7839,'1981-1-9',2450,null,10 12 7788,scott,analyst,7566,'1982-12-9,3000,null,20 13 7839,king,president,null,'1981-11-17',5000,null,10 14 7844,turner,salesman,7698,'1981-12-8',1500,0,30 15 7876,adams,clerk,7788,'1983-1-12',1100,null,20 16 7900,james,clerk,7698,'1981-12-3',950,null,30 17 18 --7902,ford,analyst,7566,'1981-12-3',3000,null,20 19 20 7934,miller,clerk,7782,'1982-1-23',1300,null,10

w命令

W命令即是寫空間(write space)是對sed的輸出流里的數(shù)據(jù)輸出到指定文件的操作,該操作對原文件沒有影響。

文本內(nèi)容輸出(寫)

內(nèi)容輸入到新文件并回顯控制臺

# 1 將文件infilename的內(nèi)容輸出到outfilename里同時打印在控制臺上。sed 'w outfilename' infilename# 示例,將emp.txt的內(nèi)容寫入outfile.txt并屏顯 sed 'w outfile.txt' emp.txt

內(nèi)容輸入到新文件并屏不回顯

# 2 將文件infilename的內(nèi)容輸出到outfilename里不打印在控制臺上(靜默方式)。sed -n 'w outfilename' infilename # 示例,將emp.txt的內(nèi)容寫入outfile.txt不屏顯 sed -n 'w outfile.txt' emp.txt

指定具體行號輸入到新文件

# 3 將文件infilename的第m行內(nèi)容輸出到outfilename里。 sed -n 'm w outfilename' infilename# 將mp.txt里第17行內(nèi)容輸入到文件outfile.txt內(nèi) sed -n '17 w outfile.txt' emp.txt

指定行號范圍輸入到新文件

# 4 將文件infilename的第m到n行內(nèi)容輸出到outfilename里。sed -n 'm,n w outfilename' infilename# 將mp.txt里第14到16行內(nèi)容輸入到文件outfile.txt內(nèi) sed -n '14,16 w outfile.txt' emp.txtcat outfile.txt7844,turner,salesman,7698,'1981-12-8',1500,0,307876,adams,clerk,7788,'1983-1-12',1100,null,207900,james,clerk,7698,'1981-12-3',950,null,30

指定行號到結(jié)尾輸入到新文件

# 5 將文件infilename的第m行到文件結(jié)尾內(nèi)容輸出到outfilename里。 sed -n 'm,$ w outfilename' infilename# 將emp.txt第18行到文件結(jié)尾部分輸出到outfile.txt sed -n '18,$ w outfile.txt' emp.txt#結(jié)果 --7902,ford,analyst,7566,'1981-12-3',3000,null,207934,miller,clerk,7782,'1982-1-23',1300,null,10

指定遞增行號輸入到新文件

# 6 將文件infilename的第m行以n為增幅(步長)行號的內(nèi)容輸出到outfilename里。 sed -n 'm~n w outfilename' infilename # 示例,該情況會將8、13、18行的內(nèi)容寫入新文件outfile.txt里 sed -n '8~5 w outfile.txt' emp.txt7657,kate,clerk,7902,'1989-11-11',1400,1800,10 7839,king,president,null,'1981-11-17',5000,null,10 --7902,ford,analyst,7566,'1981-12-3',3000,null,20

寫文件結(jié)合模式匹配

匹配關(guān)鍵字方式寫文件

# 1 將文件infilename的以匹配word的內(nèi)容輸出到outfilename里。sed -n '/word/ w outfilename' infilename# 匹配emp.txt文件里manager關(guān)鍵字,寫入到outfile.txt sed -n '/manager/ w outfile.txt' emp.txt

匹配關(guān)鍵字結(jié)合行號寫文件

# 2 對文件infilename針對匹配的行n開始關(guān)鍵字word,然后把內(nèi)容輸出到outfilename里。 sed -n '/word/,n w outfilename.txt' infilename# 這里先做幾個約定: # 1定義3個變量:首次匹配word的行號f、最后匹配word的行號l、參數(shù)行號n。 # 2 這里遍歷的關(guān)鍵字是”manager”即尋找所有的經(jīng)理們。 # 3 由假設(shè)1和2則f是4,l是11。# 2_1 當n小于等于f時,這里4小于等于4,僅顯示匹配到manager的行。 sed -n '/manager/,4 w outfile.txt' emp.txt #cat outfile.txt 7566,jones,manager,7839,'1981-4-2',2975,null,20 7655,jack,manager,7698,'1987-3-28',1600,1800,10 7698,blake,manager,7839,'1981-5-1',2850,null,30 7782,clark,manager,7839,'1981-1-9',2450,null,10# 2_2 如果n>f且n<l,不難發(fā)現(xiàn)即有第4到7行內(nèi)容和7行后僅匹配“manager“的內(nèi)容匯總而來。 # 即這種情況下的文本內(nèi)容由f行到n行的內(nèi)容和n行后僅匹配manager的行兩部分組成。 sed -n '/manager/,7 w outfile.txt' emp.txt #cat outfile.txt 7566,jones,manager,7839,'1981-4-2',2975,null,20 7654,martin,salesman,7698,'1981-9-28',1250,1400,30 7655,jack,manager,7698,'1987-3-28',1600,1800,10 7656,tim,clerk,7902,'1982-12-12',1400,1400,30 7698,blake,manager,7839,'1981-5-1',2850,null,30 7782,clark,manager,7839,'1981-1-9',2450,null,10# 2_3 如果 n>=l,數(shù)據(jù)來自行4和12之間,即內(nèi)容是f行到n行。 sed -n '/manager/,12 w outfile.txt' emp.txt #cat outfile.txt 7566,jones,manager,7839,'1981-4-2',2975,null,20 7654,martin,salesman,7698,'1981-9-28',1250,1400,30 7655,jack,manager,7698,'1987-3-28',1600,1800,10 7656,tim,clerk,7902,'1982-12-12',1400,1400,30 7657,kate,clerk,7902,'1989-11-11',1400,1800,10 7698,blake,manager,7839,'1981-5-1',2850,null,30 7699,dlake,salesman,7839,'1983-6-15',3000,null,10 7782,clark,manager,7839,'1981-1-9',2450,null,10 7788,scott,analyst,7566,'1982-12-9,3000,null,20

關(guān)鍵字首次出現(xiàn)行寫文件

# 3 對文件infilename關(guān)鍵字word首次出現(xiàn)的行直至文末的內(nèi)容輸出到outfilename里。 sed -n '/word/,$ w outfilename' infilename# 將emp.txt文件里首次出現(xiàn)的行寫入到文件outfile.txt內(nèi)。 sed -n '/manager/,$ w outfile.txt' emp.txt # cat outfile.txt 7566,jones,manager,7839,'1981-4-2',2975,null,20 7654,martin,salesman,7698,'1981-9-28',1250,1400,30 7655,jack,manager,7698,'1987-3-28',1600,1800,10 7656,tim,clerk,7902,'1982-12-12',1400,1400,30 7657,kate,clerk,7902,'1989-11-11',1400,1800,10 7698,blake,manager,7839,'1981-5-1',2850,null,30 7699,dlake,salesman,7839,'1983-6-15',3000,null,10 7782,clark,manager,7839,'1981-1-9',2450,null,10 7788,scott,analyst,7566,'1982-12-9,3000,null,20 7839,king,president,null,'1981-11-17',5000,null,10 7844,turner,salesman,7698,'1981-12-8',1500,0,30 7876,adams,clerk,7788,'1983-1-12',1100,null,20 7900,james,clerk,7698,'1981-12-3',950,null,30--7902,ford,analyst,7566,'1981-12-3',3000,null,207934,miller,clerk,7782,'1982-1-23',1300,null,10

成對關(guān)鍵字匹配寫文件

# 4 infilename里匹配兩個關(guān)鍵詞word1、word2之間的內(nèi)容,當word1、word2出現(xiàn)多次時,按照word1、word2對進行匹配。輸出到outfilename sed -n '/word1/,/word2/ w outfilename' infilename # 示例,成對匹配兩個關(guān)鍵字并輸出到文件。 sed -n '/manager/,/salesman/ w outfile.txt' emp.txt [root@host_128 shellscrips]# cat outfile.txt 7566,jones,manager,7839,'1981-4-2',2975,null,20 7654,martin,salesman,7698,'1981-9-28',1250,1400,30 7655,jack,manager,7698,'1987-3-28',1600,1800,10 7656,tim,clerk,7902,'1982-12-12',1400,1400,30 7657,kate,clerk,7902,'1989-11-11',1400,1800,10 7698,blake,manager,7839,'1981-5-1',2850,null,30 7699,dlake,salesman,7839,'1983-6-15',3000,null,10 7782,clark,manager,7839,'1981-1-9',2450,null,10 7788,scott,analyst,7566,'1982-12-9,3000,null,20 7839,king,president,null,'1981-11-17',5000,null,10 7844,turner,salesman,7698,'1981-12-8',1500,0,30

關(guān)鍵字匹配及隨后行寫文件

# 5 infilename里匹配兩個關(guān)鍵詞word所在行和隨后的m行內(nèi)容,輸出到outfilename。 sed -n '/word1/,+m w outfilename' infilename#示例 匹配clerk關(guān)鍵字并將隨后的2行寫入文件outfile.txt內(nèi)。 sed -n '/clerk/,+2 w outfile.txt' emp.txt [root@host_128 shellscrips]# cat outfile.txt 7369,smith,clerk,7902,'1980-12-17',800,null,20 7499,allen,salesman,7698,'1981-2-20',1600,300,30 7521,ward,salesman,7698,'1981-2-22',1250,500,30 7656,tim,clerk,7902,'1982-12-12',1400,1400,30 7657,kate,clerk,7902,'1989-11-11',1400,1800,10 7698,blake,manager,7839,'1981-5-1',2850,null,30 7876,adams,clerk,7788,'1983-1-12',1100,null,20 7900,james,clerk,7698,'1981-12-3',950,null,307934,miller,clerk,7782,'1982-1-23',1300,null,10

總結(jié)

以上是生活随笔為你收集整理的Linux sed 写命令常见使用案例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。