sed-文本增加内容常用方法记录

上一篇 / 下一篇  2013-10-30 16:21:04 / 个人分类:linux

范例文本
[root@nagios test]# cat quote.txt 
The honeysuckle band polayed al ngint long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Misss P.Neave was in attendance.

1、第n行前面增加内容(ni)
例子:第一行前面增加内容
[root@nagios test]# sed '1i --->APPEDED TEXT<---' quote.txt
--->APPEDED TEXT<---
The honeysuckle band polayed al ngint long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Misss P.Neave was in attendance.

2、第n行后面增加内容(na)
例子:第三行后面增加内容
[root@nagios test]# sed '3a --->APPEDED TEXT<---' quote.txt   
The honeysuckle band polayed al ngint long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
--->APPEDED TEXT<---
The local nurse Misss P.Neave was in attendance.

例子:最后一行后面增加内容
[root@nagios test]# sed '$a --->APPEDED TEXT<---' quote.txt  
The honeysuckle band polayed al ngint long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Misss P.Neave was in attendance.
--->APPEDED TEXT<---


3、附加文本(a)
[root@nagios test]# sed '/^Too/a\ --->APPEDED TEXT<---' quote.txt  
The honeysuckle band polayed al ngint long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
 --->APPEDED TEXT<---
The local nurse Misss P.Neave was in attendance.


4、插入文本(i)
[root@nagios test]# sed '/^Too/i\ ---> INSERTED TEXT<---' quote.txt 
The honeysuckle band polayed al ngint long for only $90.
It was an evening of splendid music and company.
 --->INSERTED TEXT<---
Too bad the disco floor fell through at 23:10.
The local nurse Misss P.Neave was in attendance.

5、修改文本(c)
[root@nagios test]# sed '/^Too/c\ --->CHANGED TEXT<---' quote.txt              
The honeysuckle band polayed al ngint long for only $90.
It was an evening of splendid music and company.
 --->CHANGED TEXT<---
The local nurse Misss P.Neave was in attendance.


6、删除文本(d)
[root@nagios test]# sed -e '1,3d' quote.txt  
The local nurse Misss P.Neave was in attendance.
说明:删除1-3行文本

sed -n '$!N;/pattern/!P;D' file
删除匹配行前一行


TAG: 文本 增加 sed 内容 常用 方法

 

评分:0

我来说两句

Open Toolbar