AIX下使用sed对文件进行操作

一.附加文本
使用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.


三.添加文本
在指定行前面添加数据。
[address] i\
text\
text\
...
text
address:行位置,可以使用正则表达式定位,也可以直接指定行号。
text:为替换的文本,注意最后一行没有"\"。
例如:
pg insert.sed
# insert data
/.*ing/ i\
[the insert date].
运行脚本:
sed -f insert.sed quote.txt
The honeysuckle band played all night long for noly $90.
[the insert date].
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.

四.删除文本
删除文件中的内容,删除1行或者连续的多行,或者匹配某个模式的所有行格式:
[address[,address]] d
文件信息:
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.

删除第一行:
sed '1d' quote.txt
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.

删除第一到第三行:
sed '1,3d' quote.txt
The local nurse Miss P.Neave was in attendance.

删除最后一行:
sed '$d' 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.

使用正则表达式删除符合条件的所有行。
删除包含以字母a开头,以字母d结尾,总长为3的行,符合的有第一行(band),第二行(and)。
sed '/a.d/d' quote.txt
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.

五.替换文本,格式:
[address[,address]] s/pattern-to-find/replacement-pattern/[g p w n]
address:可选项
g:默认情况下,只替换首次出现模式,使用g替换全部所有出现的模式;
p:默认sed将所有被替换行写入标准输出,加p将使-n选项无效;-n不打印输出结果。
w:文件名,使用此选项将输出定向到一个文件。

将小写的floor替换为大写的FLOOR:
sed 's/floor/FLOOR/' 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.

将包含"$"符号的行$符号删除掉:
包含$符号的行:
sed -n '/$.*/p' quote.txt
The honeysuckle band played all night long for noly $90.
删除$(使用空替代)符号后的行:
sed -n 's/\$//p' quote.txt
The honeysuckle band played all night long for noly 90.

将首次出现的"The"单词替换为"THE",并输出到另一个文件中
若sed.out存在则向里面写数据,所不存在则创建此文件并写数据:
sed 's/The/THE/w sed.out' 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.
查看文件sed.out:
pg sed.out
THE honeysuckle band played all night long for noly $90.
THE local nurse Miss P.Neave was in attendance.

使用替换修改字符串
&命令实现字符串的添加:
sed -n 's/nurse/"Hello" &/p' quote.txt
The local "Hello" nurse Miss P.Neave was in attendance.

将sed结果写入文件命令
格式:[address[,address]]w filename
把带"of"的行写入文件sed.out中:
sed -n '/of/w sed.out' quote.txt
pg sed.out
It was an evening of splendid music and company.

从文件中读取数据
将要添加的数据:
pg sedex.txt
中文
执行命令,添加数据:
sed '/company./r sedex.txt' 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.

--匹配后退出,格式:add
包含The单词的行:
sed -n '/The/p' quote.txt
The honeysuckle band played all night long for noly $90.
The local nurse Miss P.Neave was in attendance.
打印首次出现The的行:
sed '/The/q' quote.txt
The honeysuckle band played all night long for noly $90.

显示文件中的控制字符
cat -v func.txt
at the begining^[[C^[[B^[[B^[[A
The file name is func,
^H
and it's suffix is .txt^[[D^[[D
查看这些控制字符对应的ASCII码:
sed -n '1,$l' func.txt
at the begining\033[C\033[B\033[B\033[A$
The file name is func,$
\b$
and it's suffix is .txt\033[D\033[D$
\b:表示空格,
$:表示行末尾
\033:表示退格键

--the end--

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值