linux sed 写文件,使用sed对文件进行操作
使用sed對文件進行操作
[日期:2012-12-07]
來源:Linux社區
作者:bobo12082119
[字體:大 中 小]
一.附加文本
使用a\在指定行后面附加1行或多行;若不指定放置的位置,則默認放到每一行的后面。
附加文本時,不允許指定范圍,只允許一個地址模式。
附加格式:
[address] a\
text\
text\
...
text
注意:
1.a\通知sed對a\后面的內容進行附加操作。
2.每行后面都有"\",當sed執行到\時,將創建一個新行,并將內容添加進去。
3.最后一行不能有"\"。
例子:
如果將附加的第一行最后的"\"去掉,那么運行腳本將會報錯。
pg append.sed
#!bin/sed -f
# append text
/company/ a\
Then suddenly it happened.
_yeeXun.
執行之后:
sed -f append.sed quote.txt
sed: Unrecognized command: _yeeXun.
修改之后
pg append.sed
#!bin/sed -f
# append text
/company/ a\
Then suddenly it happened.\
_yeeXun.
執行腳本:
sed -f append.sed quote.txt
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
Then suddenly it happened.
_yeeXun.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.
因為sed不與原文件打交道,編輯的只是其一個拷貝,所以我們附加的數據不會寫到文件中;
當執行上面的命令之后,我們查看下quote.txt文件:
pg quote.txt
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.
二.修改文本
使用c\修改指定行,格式:
[address[,address]] c\
text\
text\
...
text
address:行位置,可以使用正則表達式定位,也可以直接指定行號。
text:為替換的文本,注意最后一行沒有"\"。
例子
替換第三行數據:
pg change.sed
# change date
/bad/ c\
[Two good.]\
next goods.
運行腳本:
sed -f change.sed quote.txt
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
[Two good.]
next goods.
The local nurse Miss P.Neave was in attendance.?
總結
以上是生活随笔為你收集整理的linux sed 写文件,使用sed对文件进行操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 233乐园广告太多怎么办
- 下一篇: linux find查找文件然后删除,l