2010-04-22 26 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 
相關問題