打开高效文本编辑之门_调用Linux的sed命令
生活随笔
收集整理的這篇文章主要介紹了
打开高效文本编辑之门_调用Linux的sed命令
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ? ? ? ? ? ? ? ? ? ? ? Linux sed命令執行方式匯總案例
聲明與簡介
sed:Stream Editor文本流編輯,sed是一個“非交互式的”面向字符流的編輯器。Sed的命令執行主要介紹如何引用sed腳本內容或者執行內置sed解釋器的腳本,命令執行的詳細使用說明見下文。本文主要以實際的數據來介紹sed的命令行執行方式。
數據說明
如下是使用的示例文本數據,這里的行號是為了方便解釋結果,不是文本內容。
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,scott 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命令行執行
單行執行多條命令
# 1 找出含有scoot、james、martin的行 sed -n -e '/scott/p' -e '/james/p' -e '/martin/p' emp.txt# 執行結果 7654,martin,salesman,7698,'1981-9-28',1250,1400,30 7788,scott,analyst,7566,'1982-12-9',3000,null,20 7900,james,clerk,7698,'1981-12-3',950,null,30#注: 這里加了-n,即只顯示受影響的行。反斜杠執行多行多條命令
# 2 通過反斜杠的方式將多個命令羅列在一起串聯執行。 sed -n -e '/scott/p' \ -e '/james/p' \ -e '/martin/p' \ emp.txt# 結果 7654,martin,salesman,7698,'1981-9-28',1250,1400,30 7788,scott,analyst,7566,'1982-12-9',3000,null,20 7900,james,clerk,7698,'1981-12-3',950,null,30#注:多行執行需要加上”\”讓命令換行。大括號執行多行多條命令
# 3 將命令包裹在大括號內 sed -n '{ /scott/p /james/p /martin/p }' emp.txt#注: 這里的文件名emp.txt不能換行輸出,即需要和“}’”在一行上。腳本文件僅含處理邏輯
引用腳本文件方式調用
# 1 定義文件,sed的處理邏輯定義在該文件內,外部仍已sed來調用。 # Step 1 創建個sed腳本文件,這里后綴名不一定是.sed。 vi replace_demo.sed# Step 2 輸入如下內容: /scott/p /james/p /martin/p# Step 3 賦予可執行權限 Chmod +x ?replace_demo.sed# Step 4 調用腳本文件 sed -nf ./replace_demo.sed emp.txt# 結果如下 7654,martin,salesman,7698,'1981-9-28',1250,1400,30 7788,scott,analyst,7566,'1982-12-9',3000,null,20 7900,james,clerk,7698,'1981-12-3',950,null,30#注:這里調用時需要寫 –nf而不是 –fn,即n必須在前。不然會報錯sed: couldn't open file n: No such file or directory腳本內注釋
# sed腳本內通過”#”來注釋,針對上例Step2,我們注釋第2行。再次執行查看效果 /scott/p #/james/p /martin/p#結果如下所示,即第二行未執行。 7654,martin,salesman,7698,'1981-9-28',1250,1400,30 7788,scott,analyst,7566,'1982-12-9',3000,null,20腳本文件含解釋器執行
腳本文件直接執行
#該方式直接調用腳本文件,無需外部加sed命令,因為腳本文件里已經內置sed解釋器。# Step 1 定義個sed的腳本文件,比如這里命名為replace_interpreter.sed vi replace_interpreter.sed# Step 2 在replace_interpreter.sed文件里加入如下內容 #!/bin/sed -nf /scott/p #/james/p /martin/p# Step 3 給replace_interpreter.sed賦予可執行權限 chmod +x replace_interpreter.sed# Step 4 調用腳本文件 ./replace_interpreter.sed emp.txt# 結果如下: 7654,martin,salesman,7698,'1981-9-28',1250,1400,30 7788,scott,analyst,7566,'1982-12-9',3000,null,20總結
以上是生活随笔為你收集整理的打开高效文本编辑之门_调用Linux的sed命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《后浪》任新正每次都会弄个手势,这个手势
- 下一篇: 如果上海以后发生比5月24日内环高架宜山