在命令行下将一串字符插入到一个文本文件的某一行当中

王朝other·作者佚名  2008-05-18
窄屏简体版  字體: |||超大  

在.sh脚本文件中,需要修改一个文本文件里面的内容,即把一串字符插入到该文本文件的第N行,实现方法如下:

insert strings "foo" into line 5 of src.txt

tips 1:

awk '{if(NR!=5) print $0; else printf ( "foo\n%s\n", $0 ); }' src.txt

tips 2:

use head and tail command can also get the same result, but it is a little complicated

呵呵`~太妙了!谢谢coredump!

我后来也想到了一个方法:

sed '3ithere input your content,that will insert 3th line' ~/yourfile>~/yourfile.tmp

sed 'w ~/yourfile' ~/yourfile.tmp

rm -f ~/yourfile.tmp

但是上面这个方法好像很不方便~~这下可好了,呵呵!

不过我发觉coredump的方法并不能更新源文件,只是把插入后的结果写入缓冲区并输出到屏幕~

如果要更新源文件怎么办???

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航