+ -
当前位置:首页 → 问答吧 → 特定字符串后面追加后缀

特定字符串后面追加后缀

时间:2011-07-15

来源:互联网

我有一个文件test, 内容如下:
====
$title
MCO,Onh
$symmetry c1
$coord    file=coord   data
$scfintunit
unit=30       size=0       file=twoint
$scfiterlimit   300
$forceapprox    file=forceapprox  fdsal
$interconversion  off
   qconv=1.d-10
   maxiter=25
$coordinateupdate
....
====

想把这里面的某行下面添加一会注释以及file=xxx 字符串后面添加一个后缀, 比如'.dat'

我写了个脚本superadd.pl 如下:

#!/usr/bin/perl
$^I=".bak";
#$supperadd = 'file'
while (<>){
if(/\$coord  /){

$_ .= "\$kollman\n";
}
if (/file=/){
$_ .= ".dat";

}

print;
}


但是,'.dat' 后缀总是添加在下一行.

尝试用
s/file=coord/file=coord.dat/;
s/file=twoint/file=twoint.dat/;
方式解决,但无奈这个记事本中的file=太多, 也不固定(有的文件中有30项,有的有40项,名称也不一样).

那位高手我用什么方式在后面追加后缀?

另外, 我想通过命令行方式把要添加的后缀加上, 比如:
perl superadd.pl <.dat> test
的方式输入后, 自动把test文件里面的内容修改为:
====
$title
MCO,Onh
$symmetry c1
$coord    file=coord.dat   data
$scfintunit
unit=30       size=0        file=twoint.dat
$scfiterlimit   300
$forceapprox    file=forceapprox.dat fdsal
$interconversion  off
   qconv=1.d-10
   maxiter=25
$coordinateupdate
....
====

怎么修改?

多谢!!

作者: tcclab   发布时间: 2011-07-15

perl -i.bak -pe 's/\b(?<=file=)(\w+)\b/$1.dat/g' Ur_Test_File

作者: Kitaisky   发布时间: 2011-07-20