linux sed 冒号,linux sed 总结
1.簡介
sed是一種行編輯器,它一次處理一行內(nèi)容。
2.sed調(diào)用方式
sed [options] 'command' file(s)
sed [options] -f scriptfile file(s)
第一種直接在命令行中執(zhí)行,第二種把命令寫到了腳本中,二者無本質(zhì)區(qū)別。
示例(1):打印hello.txt的內(nèi)容
sed -n p hello.txt
說明:
-n:sed會在處理一行文本前,將待處理的文本打印出來,-n參數(shù)關(guān)閉了這個(gè)功能
p:命令表示打印當(dāng)前行
hello.txt:待處理的文件
這個(gè)指令相當(dāng)于cat
3.定址
告訴sed你期望處理的行,由逗號分隔的兩個(gè)數(shù)字表示,$符號表示最后一行;
當(dāng)然也可以使用正則來定位期望處理的行。
示例(2):打印hello.txt的第二行到最后一行
sed -n '2,$'p hello.txt
示例(3):打印hello.txt中正則匹配"100"的行
sed -n '/100/'p hello.txt
4.基本命令
hello.txt的內(nèi)容為
1 2 3
10 20 30
100 200 300
命令:a
在匹配行的后面加入一行文本
示例(4)匹配100的行,后面加入一行"new line"
sed '/100/'a\ "new line" hello.txt
輸出內(nèi)容為:
1 2 3
10 20 30
100 200 300
new line
命令:i
在匹配行的前面加入一行文本
示例(5)匹配100的行,前面加入一行"new line"
sed '/100/'i\ "new line" hello.txt
輸出內(nèi)容為:
1 2 3
10 20 30
new line
100 200 300
命令:c
將匹配行替換為目的行
示例(5)匹配100的行,替換為"new line"
sed '/100/'c\ "new line" hello.txt
輸出內(nèi)容為:
1 2 3
10 20 30
new line
命令:d
將匹配行刪除
示例(5)刪除匹配100的行
sed '/100/'d hello.txt
輸出內(nèi)容為:
1 2 3
10 20 30
命令:s
將匹配行替換
詳細(xì)命令為:s/pattern-to-find/replacement-pattern/g
pattern-to-find:被替換的串
replacement-pattern:替換成這個(gè)串
g:全部替換,默認(rèn)只替換匹配到的第一個(gè)
示例(5)講100替換為hello
sed 's/100/hello/g' hello.txt
輸出內(nèi)容為:
1 2 3
10 20 30
hello 200 300
5.元字符集
^:匹配一行的開始
$:匹配一行的結(jié)束
.:匹配某個(gè)字符
[abc]:匹配指定范圍字符
6.實(shí)用命令
匹配以10開頭的行,并替換為yes,并輸出
sed -n 's/^10/yes/p' hello.txt
輸出內(nèi)容為:
yes 20 30
yes0 200 300
取出文件中行手的行號與冒號
設(shè)hello.txt的內(nèi)容為
1:#!/bin/sh
2:cat hello.txt
3:exit
命令:
sed -n -e 's/^[0-9]\{1,\}://g'p hello.txt
輸出結(jié)果為:
#!/bin/sh
cat hello.txt
exit
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的linux sed 冒号,linux sed 总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 后发送过慢的问题_点胶阀使用常见问题怎么
- 下一篇: yii 引用php文件,Yii中引出ph