This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Perl: How do you insert numbers after grouping variable?
I have the following perl one liner:
perl -pi.bak -e 's/(.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t)/$123424977\t/g if $. <= 200'
The problem is that I want to insert the number 23424977 after the grouped regex (.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t).
But Perl thinks I'm referring to group $123424977 and doesn't recognize I mean $1 and that 23424977 is the number I want to insert afterwards. How can I correct this?
simply refer to the $1 capture group with surrounding braces: ${1}:
perl -pi.bak -e 's/(.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t)/${1}23424977\t/g if $. <= 200'
Related
This question already has answers here:
How can I use a variable in the replacement side of the Perl substitution operator?
(9 answers)
Closed 3 years ago.
I have a file containing search and replace string in a single line.
And I am reading that file, using split to separate search and replace string
and apply it on a variable.
File:
(.*) pre_$1
Perl Code:
$str = "a";
$line = < FILEHANDLE>; # Read above file.Contains (.*) pre_$1
my ($ss,$rs) = split /\s/,$line;
$str =~ s/$ss/$rs/ee;
This seems to be not working.
I tried to look online, one result is close which is wrap the replace string in both single and double quotes.
i.e.:
$rs = '"pre_$1"';
This works if its in the script, but if I read from file I done see any replacement.
Can someone point me to what I am doing wrong here?
Thanks.
s//$rs/ee expects $rs to contains valid Perl code. pre_$1 is not valid Perl code. It's a very bad idea to expect the user to provide Perl code anyway.
Solution:
use String::Substitution qw( gsub_modify );
gsub_modify($str, $ss, $rs);
This question already has answers here:
How can I extract the matches from the Perl match operator into variables?
(6 answers)
Closed 5 years ago.
The following perl code only gives back true or false (1 & 0)
#!/usr/bin/perl
use strict;
use warnings;
my $string;
$string ="interface Ethernet1/20
shutdown";
my $test = $string =~ m/^.+$(?=\s+shutdown)/mg;
print "'$test'\n";
I get back a 1.
But how can I get back the matched string 'interface Ethernet1/20' ?
Thanks for every help!
Simply give it list context:
my ($test) = $string =~ m/^.+$(?=\s+shutdown)/mg;
The concept of evaluation context (list vs scalar) is fundamental to Perl programming, so it may be time to review some tutorials and/or a reference manual.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
I am trying to understand some code which is already written..
I am not able to understand
my $l = $0; $l =~ s-/[^/]+/[^/]+$-/lib/perl-;
what is assigned to $0 ?
what is - (Hyphen) operator in the expression ?
whay $- is used in the expression?
Please explain the ablove 3Qs.
BEGIN {
# figure out our general library path
my $p;
my $l = $0; $l =~ s-/[^/]+/[^/]+$-/lib/perl-;
push #INC,$l;
# add some platform components, based on files
push #INC,"$l/sunos" if ( grep /solaris/,#INC );
push #INC,"$l/csw" if ( grep /csw/,#INC );
push #INC,"$l/i386" if ( grep /i386/,#INC );
push #INC,"$l/x86_64" if ( grep /x86_64/,#INC );
}
You can find various Perl special variables in perlvar. $0 is explained there.
As mentioned in perlop, the substitution operator s/REGEX/REPLACEMENT/ can be written with different delimiters, e.g. dashes. It's handy when the regex or replacement contain slashes, as is the case here. Using paired delimiters might be more readable:
s{/[^/]+/[^/]+$}{/lib/perl}
or, using /x:
s{ / [^/]+ / [^/]+ $ }{/lib/perl}x
This question already has answers here:
How can I parse quoted CSV in Perl with a regex?
(7 answers)
Closed 7 years ago.
I have a string that looks something like this:
'som,ething', another, 'thin#'g', 'her,e', gggh*
I am trying to get it to split on the commas that are NOT in the elements, like this:
'som,ething'
another
'thin#'g'
'her,e'
gggh*
I am using parse_line(q{,}, 1, $string) but it seems to fail when the string has single quotes in it. Is there something I'm missing?
#!/usr/bin/perl
use strict;
use warnings;
my $string = q{'som,ething', another, 'thin'g', 'her,e', gggh*};
my #splitted = split(/,(?=\s+)/, $string);
print $_."\n" foreach #splitted;
Output:
'som,ething'
another
'thin'g'
'her,e'
gggh*
Demo
It looks like you're trying to parse comma-separated values. The answer is to use Text::CSV_XS since that handles the various weird cases you're likely to find in the data. See How can I parse quoted CSV in Perl with a regex?
Using split is not the way to go. If you are sure your string is well formatted using a global match is more simple, example:
my $line = "'som,ething', another , 'thin#'g', 'her,e' , gggh*";
my #list = $line =~ /\s*('[^#']*(?:#.[^#']*)*+'|[^,]+(?<=\S))\s*/g;
print join("|", #list);
(the (?<=\S) is only here to trim items on the right)
This question already has answers here:
Perl regex: replace all backslashes with double-backslashes
(6 answers)
Closed 8 years ago.
How do I replace single backslashes in a string with double backslashes?
I've tried things such as
s/\\(?!\\)/\\\\/g
s/\\/\\\\/g
s/[^//]/\\\\/g
But they all produce multiple backslashes after each other.
So I want:
\test
to be replaced with
\\test
Edit: Sorry I should also mention that the regex is in a loop so I need a regex that only matches the string if there is ONLY ONE backslash. Once there is more than one backslash then the regex should reject the string. Apologies
The most helpful thing to note is to use a different delimiter for the regex, so things don't get jumbled by all the leaning towers:
my $str = '\test';
$str =~ s{\\}{\\\\}g;
print $str;
Outputs:
\\test
Update
Per your revised specification, if you only want to escape a single backslash, and ignore all others, then just use a negative lookahead and lookbehind assertion:
my $str = <<'END_STR';
\one \\two \\\three
END_STR
print $str;
$str =~ s{(?<!\\)\\(?!\\)}{\\\\}g;
print $str;
Outputs:
\one \\two \\\three
\\one \\two \\\three
echo '\replace' | perl -pe 's/\\/\\\\/g'
\\replace
OR with sed
# echo '\replace' | sed 's/\\/\\\\/g'
\\replace