蓝森林首页 | 返回主页 | 本站地图 | 站内搜索 | 联系信箱 |
 您目前的位置:首页 > 自由软件 > 技术交流 > 应用编程


    

蓝森林 http://www.lslnet.com 2006年6月26日 11:18


[求助]如何在指定行前面增加几行?

我想在一个文本中指定的某行前面增加几行,该如何实现,利用sed或者awk,或者其他的吧,谢谢大家指点!!

[求助]如何在指定行前面增加几行?

来个笨办法 :oops:  :oops:  :oops:只能在已知字符下加

原文件如下:

[code]# cat grade.txt
M.Tans 5/99 48311 Green 8 40 44
J.Lulu 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
J.Troll 07/99 4842 Brown-3 12 26 26
L.Tansl 05/99 4712 Brown-2 12 30 28[/code]

现在在最后一行前加上两行空行:

[code] # awk '{gsub(/L.Tansl/,"\n\nL.Tansl")}{print $0}' grade.txt
M.Tans 5/99 48311 Green 8 40 44
J.Lulu 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
J.Troll 07/99 4842 Brown-3 12 26 26


L.Tansl 05/99 4712 Brown-2 12 30 28[/code]

[求助]如何在指定行前面增加几行?

#!/bin/sed -f
4 i\
insert this line before line 4.
或者,
#!/bin/sed -f
/specified-word/ i\
insert this line before line contained specified-word.

[求助]如何在指定行前面增加几行?

-->

呵,sed这么方便啊,俺还没看呢 :oops:  :oops:  :oops:

[求助]如何在指定行前面增加几行?

多谢 wingger和chenl!!

[求助]如何在指定行前面增加几行?

用ed更简单
[code]
ed file<<!
4i
some string
.
w
!
[/code]



Copyright © 1999-2000 LSLNET.COM. All rights reserved. 蓝森林网站 版权所有。 E-mail : webmaster@lslnet.com