问:如何添加此信息$ sv?用新的替换旧的。
$ less stver.php
define('update_ver', 328);
$ less rec.pl
#!/usr/bin/perl
$sv="stver.php";
open (NUMECHO, "$sv") || die "Can't open $sv";
while (<NUMECHO>=~m/(\d+)/g) #digits output
{
chomp;
#print "$1\n";
$b=$1;
$a=$b;
++$a;
}
close (NUMECHO);
print "Old value = $b\n";
print "New value = $a\n";
当我运行脚本
时$ ./rec.pl
Old value = 328
New value = 329
我需要的是将328替换为328到stver.php
答案 0 :(得分:2)
这可以正常运行,每次执行时都会将stver.php的数字加1:
<强> rec.pl 强>:
#!/usr/bin/perl
$sv="stver.php";
open (NUMECHO, "<$sv") or die("Unable to open $sv");
open (TMP, "> $sv.tmp") or die("Unable to open $sv.tmp");
while(<NUMECHO>) {
if(/(\d+)/) {
m/(\d+)/g;
$b = $1;
$a = "$b" + "1";
s/$b/$a/;
}
print TMP $_;
}
close(NUMECHO);
close(TMP);
rename "$sv.tmp", $sv;
print "Old value = $b\n";
print "New value = $a\n";
答案 1 :(得分:1)
perl -i.bak -pe's/define\('\''update_ver'\'', \K(\d+)(?=\);)/$1+1/e' stver.php
答案 2 :(得分:0)
perl -i.bak -pe "s/define('update_ver', 328);/define('update_ver', 329);/" stver.php
您将获得包含所需信息的stver.php以及包含原始信息的名为stver.php.bak的备份。