2010-04-22 77 views

回答

0

如果有简单的UNIX编辑器ed安装,你可以说这样的事情:

echo "$line i 
$lines 
. 
w 
q 
" | ed filename.txt 

这是一个没有 “视觉” 模式VI。 $line必须是行号和$lines要插入文件的文本。

0
totallines=`cat test.h | wc -l` 
head -n $line test.h >$$.h 
echo "some text" >>$$.h 
tail -n $((totallines-line)) test.h >>$$.h 
mv $$.h head.h 


(修正)

+1

无用的使用猫;-) – topskip 2010-04-22 17:43:36

0

你可以只用awk

awk '/#error/{for(i=1;i<=NR-2;i++){print _[i]}print "new\n"_[NR-1];f=1 }!f{_[NR]=$0 }f' file > t && mv t file 
0
line=$(sed -n '/#error/=' test.h) 
line=$(($line - 2)) 
sed -i "$line s/$/\ntext-to-insert/" test.h 

sed -i "$line r filename" test.h 
0

它看起来像你工作太辛苦。为什么不插入文本而不是找到行号?例如:

 
$ sed '/#error/a\ 
> this text is inserted 
> ' test.h 

如果你想插入文本是在文件中,那就更简单了:

 
$ sed '/#error/r filename' test.h