I have variable in file of perl program. I only want to increase value vertically position of $[0] as $[1] $[2]
, as each $[0] variable right now have base 64 to utf-8 values.
I only want to change $[0] to increase value until end of file.
I tried command of perl as mentioned
perl -pi -e 's/U[0](\d+)[0].($1+1)/e' 25k.list
syntax error at -e line 1, near "](" Unmatched [ in regex; marked by <-- HERE in m/ <-- HERE ,/ at -e line 1.
Added:
some data in file:
$U[0] = "\data1\fileloader.ini";
$U[0] = "\data1\data2\crame\crame.ini";
$U[0] = "\data1\data2\data3\files\setup.exe";
$U[0] = "\data1\data2\data3\data4\WINDOWS\win.ini";
What I want, increase $U[from 0, untill it no longer occur], it should be:
$U[0] = "\data1\fileloader.ini";
$U[1] = "\data1\data2\crame\crame.ini";
$U[2] = "\data1\data2\data3\files\setup.exe";
$U[3] = "\data1\data2\data3\data4\WINDOWS\win.ini";
only affecting $U[ ]
The "Syntax error" comes from the fact that your regexp is missing the /
which should separate search pattern from replacement part. Additionally you want to execute the replacement part and I think the []
are meatn literally. So my best guess is, your s/p/r/ should be: s/U\\[0\\](\\d+)/"U[0]".($1+1)/e
The s///
expression that works for this case is:
s/\$U\[0\]/"\$U[".$c++."]"/ge
This means: Replace every occurence of $U[0]
with $U[n]
, n
being the next integer starting from 0
.
Please read: https://perldoc.perl.org/perlop.html about s/PATTERN/REPLACEMENT/
.
This seems to do what you want.
#!/usr/bin/perl
use strict;
use warnings;
my $x = 0;
while (<DATA>) {
s/U\[0]/'U[' . $x++ . ']'/e;
print;
}
__DATA__
$U[0] = "\data1\fileloader.ini";
$U[0] = "\data1\data2\crame\crame.ini";
$U[0] = "\data1\data2\data3\files\setup.exe";
$U[0] = "\data1\data2\data3\data4\WINDOWS\win.ini";
The output I get is:
$U[0] = "\data1\fileloader.ini";
$U[1] = "\data1\data2\crame\crame.ini";
$U[2] = "\data1\data2\data3\files\setup.exe";
$U[3] = "\data1\data2\data3\data4\WINDOWS\win.ini";
Like the other answers, I've basically made three fixes here.
[0]
being interpreted as a character class. s/.../.../
expression. $1
and use an incrementing variable for the replacement integer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.