shell 字符串处理-sed

sed 逐行处理数据

test.txt

this is a test str.
is 1 is 2 is 3 is 4 is 5 is 6 is 7 is 8
1234
qwer

-i 参数直接修改文件


查找包含字符的行, 参数-n , 结束符 /p
sed -n /r/p‘ test.txt

结果: " 1 this is a test str. 4 qwer "

 

替换  开始符s/, 结束符 /p

sed s/is/at/g‘ test.txt
结果: " that at a test str. at 1 at 2 at 3 at 4 at 5 at 6 at 7 at 8 1234 qwer "

插入选中行下一行文本, 开始标识字符 a\, 前置为选中行索引
sed 1a\abcd‘ test.txt
结果: " this is a test str. abcd is 1 is 2 is 3 is 4 is 5 is 6 is 7 is 8 1234 qwer "

插入选中行上一行文本, 开始标识字符 i\, 前置为选中行索引
sed 1i\abcd‘ test.txt
结果: " abcd this is a test str. is 1 is 2 is 3 is 4 is 5 is 6 is 7 is 8 1234 qwer "

 

删除指定一, 标识字符 d, 前置为行索引

sed 2d‘ test.txt
结果: " this is a test str. 1234 qwer "

 

删除指定多, 标识字符 d, 前置为行索引区间

sed 2,3d‘  test.txt
结果 " this is a test str. qwer "

 

删除指定行到末尾, 标识字符 d, 前置为行索引区间, $ 标识末尾

sed 3,$d‘  test.txt

结果: " this is a test str. is 1 is 2 is 3 is 4 is 5 is 6 is 7 is 8 "

 

shell 字符串处理-sed

上一篇:shell 字符串处理-expr


下一篇:OpenGL在Mac项目上的配置