sed编辑器基础
一、 更多的替換選項
①替換標記
root@localhost sed]# cat data4.txt This is a test of the test script. This is the second test of the test script. [root@localhost sed]# sed 's/test/trial/' data4.txt This is a trial of the test script. This is the second trial of the test script.
替換格式: ? s/pattern/replacement/flags
4種可用的替換標記:
數字:表明新聞本將替換第幾處模式匹配的地方;
g,表明新文本將會替換所有匹配的文本;
p,表明原先行的內容要打印出來;
w file,將替換的結果寫到文件中。
可以指定sed編輯器用新文本替換第幾處模式匹配的地方。
[root@localhost sed]# sed 's/test/trial/2' data4.txt This is a test of the trial script. This is the second test of the trial script. 后面第二個test被替換掉了。
用g替換標記,能替換全部
[root@localhost sed]# sed 's/test/trial/g' data4.txt This is a trial of the trial script. This is the second trial of the trial script.
p替換標記會只打印修改過的行,和-n選項(禁止輸出)一起使用。
[root@localhost sed]# cat data5.txt This is a test line. This is a different line. [root@localhost sed]# sed -n 's/test/trial/p' data5.txt This is a trial line.
②替換字符
替換(/),可以用其他字符替換字符串分隔符
[root@localhost sed]# sed -n 's!/bin/bash!/bin/csh!p' /etc/passwd root:x:0:0:root:/root:/bin/csh tq2440:x:500:500:tq2440:/home/tq2440:/bin/csh win:x:501:501::/home/win:/bin/csh
二、使用地址
sed有兩種形式的行尋址:
①以數字形式表示行區間
②用文本模式來過濾出行
[address] command
1.數字方式的行尋址
[root@localhost sed]# sed '2s/dog/cat/' data1.txt The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy cat. The quick brown fox jumps over the lazy dog.
①替換標記
替換格式: ? s/pattern/replacement/flags
4種可用的替換標記:
數字:表明新聞本將替換第幾處模式匹配的地方;
g,表明新文本將會替換所有匹配的文本;
p,表明原先行的內容要打印出來;
w file,將替換的結果寫到文件中。
可以指定sed編輯器用新文本替換第幾處模式匹配的地方。
用g替換標記,能替換全部
p替換標記會只打印修改過的行,和-n選項(禁止輸出)一起使用。
②替換字符
替換(/),可以用其他字符替換字符串分隔符
二、使用地址
sed有兩種形式的行尋址:
①以數字形式表示行區間
②用文本模式來過濾出行
[address] command
1.數字方式的行尋址
轉載于:https://www.cnblogs.com/ch122633/p/7363284.html
總結
- 上一篇: 封装运动框架多个属性
- 下一篇: windows计划任务启动bat执行ja