日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux sed 写文件,使用sed对文件进行操作

發布時間:2023/12/4 linux 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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对文件进行操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。