sed应用举例

来源:互联网 发布:php自动跳转url 编辑:程序博客网 时间:2024/06/05 10:43

1,sed '3d' test.txt   : 把test.txt中的第三行删除

2,sed ‘3a hello world’ test.txt : 在test.txt的第三行后面添加"hello world"这一行

3,sed ‘3i hello world’ test.txt : 在test.txt的第三行前面面添加"hello world"这一行

4,sed ‘3,5c hello world’ test.txt : 在test.txt的第3-5替换为hello world

4,sed -n ‘3,5p’ test.txt : test.txt的3-5行打印出来


以上是以整行进行编辑的,并且都是通过制定行的编号进行操作,下面是以行为单位对,行中的部分数据进行替换,V

5.sed ‘s/要被替换的字符串/新的字符串/g’:比如:

sed 's/^.*addr://g'  : 把开头到 addr: 这段字符串替换成 ‘ ’,即删除

sed 's/goodbye.*$//g'  : 把goodbye到结尾 这段字符串替换成 ‘ ’,即删除

注意:. *正则表达式的使用



0 0
原创粉丝点击