2013-07-09 55 views
0

请帮我这个:
文件赛后添加和删除线

我有一个文件名为:demo.txt,demo.txt的内容是:

[default] 
exten=>test1,hint,SIP/202 
exten=>tom,hint,SIP/233 

[toyota] 
exten=>test1,hint,SIP/202 
exten=>tom,hint,SIP/233 

[girls] 
exten=>test1,hint,SIP/202 
exten=>tom,hint,SIP/233 

我想脚本添加更多的行之后[丰田],我需要的东西,做这样的事情:

[default] 
exten=>test1,hint,SIP/202 
exten=>tom,hint,SIP/233 

[toyota] 
exten=>another,hint,SIP/202 
exten=>well,hint,SIP/202 
exten=>test1,hint,SIP/202 
exten=>tom,hint,SIP/233 

[girls] 
exten=>test1,hint,SIP/202 
exten=>tom,hint,SIP/233 

我需要另一个脚本删除位于下[丰田] 例线,我想德尔ETE:

exten=>well,hint,SIP/202 

请告诉我,我该怎么办,在一个简单干净的方式

谢谢!

回答

0

如果sed的一个选项:

要添加的行

sed '/^\[toyota\]$/ aexten=>another,hint,SIP/202\nexten=>well,hint,SIP/202' demo.txt 

要删除的行

sed '/^\[toyota\]$/, /^\[/ {/exten=>well,hint,SIP\/202/d}' demo.txt 
+0

嗨它工作正常,但它可以删除使用SED。它说错误。 它说: [根@ Linux的SHM] sed的#SH del.sh :-e表达#1,炭45:未知命令:'2' [根@ Linux的SHM]# – Manuel

+0

@Manuel,糟糕。 ..要在202之前转义'/',请现在尝试编辑的答案 – iruvar

+0

它的工作原理!非常感谢! – Manuel

2

不知道,如果你想从另一个文件或只需添加更多的行手动输入它们。如果是后者,那么你可以做以下增加新线路:

awk '/^\[toyota\]/{print $0; print "exten=>another,hint,SIP/202" RS "exten=>well,hint,SIP/202";next}1' demo.txt 

要删除某些行:

awk '/^\[toyota\]/{p=1}p&&/exten=>well,hint,SIP\/202/{p=0;next}1' demo.txt 
+0

好吧,不是它的作品。谢谢! – Manuel

+1

@曼努埃尔你的意思是**不**工作或它**工作? :) –

+5

+1不适用。谢谢! :) – Endoro