当前位置: 技术问答>linux和unix
请问如何在文本文件中的指定位置再添加一行
来源: 互联网 发布时间:2015-04-22
本文导语: a | #name addAline #usage: addAline theFileYouWantChange whereYouWanttoAdd whatYouWantAdd #! /usr/bin/perl $srcFile = $ARGV[0]; $lineNum = $ARGV[1]; $whatYouWantToAdd = $ARGV[2]; open(SRC, $srcFile) or die "can not open the src...
a
|
#name addAline
#usage: addAline theFileYouWantChange whereYouWanttoAdd whatYouWantAdd
#! /usr/bin/perl
$srcFile = $ARGV[0];
$lineNum = $ARGV[1];
$whatYouWantToAdd = $ARGV[2];
open(SRC, $srcFile) or die "can not open the src file";
open(DIST,">tmp") or die "can not create tmp";
$i = 0;
select DIST;
while(){
if($i != $lineNum)
print $_;
}else{
print "$whatYouWantToAddn";
}
$i++;
}
close SRC;
close DIST;
´cp tmp $srcFile´;
´rm tmp´;
#usage: addAline theFileYouWantChange whereYouWanttoAdd whatYouWantAdd
#! /usr/bin/perl
$srcFile = $ARGV[0];
$lineNum = $ARGV[1];
$whatYouWantToAdd = $ARGV[2];
open(SRC, $srcFile) or die "can not open the src file";
open(DIST,">tmp") or die "can not create tmp";
$i = 0;
select DIST;
while(){
if($i != $lineNum)
print $_;
}else{
print "$whatYouWantToAddn";
}
$i++;
}
close SRC;
close DIST;
´cp tmp $srcFile´;
´rm tmp´;
|
sed '/your string/ a
your new string' yourfile
在yourfile中含有your string的行后插入your new string.
具体可以可以man sed查看插入命令i和文本附加命令a
your new string' yourfile
在yourfile中含有your string的行后插入your new string.
具体可以可以man sed查看插入命令i和文本附加命令a