using slash in Perl Command - perl

Below the command i want to run.
perl -pi -w -e 's//apps/LIVE/appl/xx/11.5.0//$XXTOP//g;' prog.txt
Here, source and replacement both have slashes in them.
How to handle this?
--
Update
I tried with curly braces and tilde that was suggested.
perl -pi -w -e 's{apps/LIVE/appl/xx/11.5.0}{$XXTOP}g;' prog.txt
In this case, Dollar sign in giving issue, else it works fine..
Error:
Name "main::XXTOP" used only once: possible typo at -e line 1.
Use of uninitialized value at -e line 1, <> chunk 1.
Use of uninitialized value at -e line 1, <> chunk 2.

Use another delimiter:
perl -pi -w -e 's~/apps/LIVE/appl/xx/11.5.0/~$XXTOP/~g;' prog.txt
You could access environment variable thru %ENV, like:
perl -pi -w -e 's~/apps/LIVE/appl/xx/11.5.0/~$ENV{XXTOP}/~g;' prog.txt

Do you want the text $XXTOP to appear in the output. In which case escape the $ - {\$XXTOP}
perl -pi -w -e 's{apps/LIVE/appl/xx/11.5.0}{\$XXTOP}g;' prog.txt
Perl is thinking that $XXTOP is a Perl variable.
Of if you are want to expand an environment variable, use $ENV{$XXTOP}

solved:
Solution:
perl -pi -w -e 's{apps/LIVE/appl/xx/11.5.0}{\$XXTOP}g;' prog.txt

Related

Perl Search and Replace Command Not Working When Called from Inside another Perl Script

I am a beginner with Perl. I am using the Below Perl Command To Search and Replace "/$" Sequence in my tcl Script. This works well When used on the linux Command Line directly.
perl -p -i -e 's/\/\$/\/\\\$/g' sed_list.tcl
I am calling to Call the above Perl One liner in another Perl script using System Command and only with " ` " Back Tick.
system(`perl -p -i -e 's/\/\$/\/\\\$/g' sed_list.tcl`);
`perl -p -i -e 's/\/\$/\/\\\$/g' sed_list.tcl`;
I am getting the Below error. Please Help With this issue.
Bareword found where operator expected at -e line 1, near "$/g"
(Missing operator before g?)
Final $ should be \$ or $name at -e line 1, within string
syntax error at -e line 1, near "s//$/"
Execution of -e aborted due to compilation errors.
I Dont Know if I Can use any other Separation Operator like % and # just like SED command but, When I used '%' operator for separation, I didn't see error but job is not done.
`perl -p -i -e 's%\/\$%\/\\\$%g' sed_list.tcl`;
I couldn't find sufficient results for this particular issue of '$' variable on the web. Any help is appreciated.
Some one here Suggested that I should Escape all Back Slashes while using System Command or calling another command using BackTicks from inside a perl script. But later they have deleted their answer. It worked for me. I would like to thank every one for taking effort and helping me out in solving my question.
Here is the correct working code.
`perl -p -i -e 's/\\\/\\\$/\\\/\\\\\\\$/g' sed_list_gen.tcl`;
or Use System function as shown Below
system("perl -p -i -e 's/\\\/\\\$/\\\/\\\\\\\$/g' sed_list_gen.tcl");
Thanks once again for the community for helping me out. . .
You can execute an external command by passing the command to a system function or by using backticks(``) operator. Please pass the command to the system() function as a string:
system(q{perl -p -i -e 's/\/\$/\/\\\$/g' sed_list.tcl})
or use backticks as:
`perl -p -i -e 's/\/\$/\/\\\$/g' sed_list_gen.tcl`
Edit:
As suggested by Paul in the comments.

Replacing a character from a file by Perl script

I am trying to replace a windows line ending character \r\n with linux style line ending character \n in a text file using a perl script. What I have written in the script is as the following:
system("perl -pi -e s/\r\n/\n/g input.txt");
And the resulting warning is:
Substitution pattern not terminated at -e line 1
I'd glad if I learn where this command is wrong.
Edit: When I used:
system("perl -pi -e 's/\r\n/\n/g' words.txt");
I got this warning:
Can't find string terminator "'" anywhere before EOF at -e line 1
Thanks in advance.
Separate the args for your system call. That will help you avoid needing to use the proper quoting and escaping:
system('perl', '-i', '-pe', 's/\r\n/\n/g', 'input.txt');
However, I'd recommend just doing this with $INPLACE_EDIT:
my $file = 'input.txt';
local #ARGV = $file;
local $^I = '';
while (<>) {
s/\R/\n/;
print;
}
perl -p -i -e 's/\r\n/\n/g' input.txt
You are missing '' in your regex. hence the error Substitution pattern not terminated at -e line 1
you are running the script on Windows, so you have to change the simple quotes and double quotes, and define the extension of the -i flag.
system('perl -i.bak -p -e "s/\r\n/\n/g" words.txt');

Why the result of encode_base64 doesn't correspond to decode_base64?

$ perl -MMIME::Base64 -e 'print encode_base64("syn_ack#163.com");'
c3luX2Fjay5jb20=
$ perl -MMIME::Base64 -e 'print decode_base64("c3luX2Fjay5jb20=");'
syn_ack.com
The encode result cannot decode to original string, why?
You have to escape # as \#or use different quotes.
This is because double quotes are expanded, and #163 is treated as an array #163 (even if this name is not valid identifier).
This works as expected:
perl -MMIME::Base64 -e "print encode_base64('syn_ack#163.com');"
c3luX2Fja0AxNjMuY29t
perl -MMIME::Base64 -e 'print encode_base64("syn_ack\#163.com");'
c3luX2Fja0AxNjMuY29t
perl -MMIME::Base64 -e "print decode_base64('c3luX2Fja0AxNjMuY29t');"
syn_ack#163.com
Switch the quotes. Perl will interpolate variables when using double-quotes.
$ perl -MMIME::Base64 -e "print encode_base64('syn_ack#163.com');"
c3luX2Fja0AxNjMuY29t
$ perl -MMIME::Base64 -e "print decode_base64('c3luX2Fja0AxNjMuY29t');"
syn_ack#163.com
http://perlmeme.org/howtos/using_perl/interpolation.html
When you see unexpected results with Perl, make sure warnings are enabled.
$ perl -w -MMIME::Base64 -e 'print encode_base64("syn_ack#163.com");'
Possible unintended interpolation of #163 in string at -e line 1.
c3luX2Fjay5jb20=
No interpolation occurs inside single-quoted ('') strings, so you could run
perl -w -MMIME::Base64 -e 'print encode_base64('syn_ack#163.com');'
or leave the double-quotes ("") and escape the #
perl -w -MMIME::Base64 -e 'print encode_base64("syn_ack\#163.com");'
Either outputs
c3luX2Fja0AxNjMuY29t
Decoding gives
$ perl -w -MMIME::Base64 -e 'print decode_base64("c3luX2Fja0AxNjMuY29t");'
syn_ack#163.com

Search for bash for loop syntax using perl command

I have several bash scripts that need to be modified and I would very much prefer to not do it by hand... basically, they all contain the line
for ((i=${BEGIN} ; i < ${END} ; i++))
and I need to change this to
for ((i=${BEGIN}-1 ; i < ${END} ; i++))
the i=${BEGIN} is unique and appears only once in each file, so I figured I could search and replace it using a simple perl command. What I came up with is
> perl -w -i -p -e "s/i=\$\{BEGIN\}/i=\$\{BEGIN\}-1/" Script.sh
which results in the following error
syntax error at -e line 1, near "{BEGIN"
syntax error at -e line 1, near "}continue"
Execution of -e aborted due to compilation errors.
What is the syntax error here?
Thanks!
Tsadkiel
Use apostrophes instead of double quotes:
perl -w -i -p -e 's/i=\$\{BEGIN\}/i=\$\{BEGIN\}-1/'
This way, backslashes aren't removed by shell, so perl sees them and they escape what they should escape.
The bash shell is performing interpolation on the argument "s/i=\$\{BEGIN\}/i=\$\{BEGIN\}-1/" before it gets to Perl. Let's see how that might work:
$ echo "s/i=\$\{BEGIN\}/i=\$\{BEGIN\}-1/"
s/i=$\{BEGIN\}/i=$\{BEGIN\}-1/
The substitution s/i=$\{BEGIN\}/i=$\{BEGIN\}-1/ is going to be a problem in Perl because Perl will treat the sequence $\{ as the start of a lookup on the hash variable %\, but it will fail to compile because it won't find an (unescaped) closing brace. So what you really want Perl to see is something like
s/i=\$\{BEGIN\}/i=\$\{BEGIN\}-1/
And there are at least two ways to change your original command-line to accomplish this:
Escape the dollar signs:
perl -wpi -e "s/i=\\\$\{BEGIN\}/i=\\\$\{BEGIN\}-1/"
Prefer single quotes, which aren't interpolated in bash:
perl -wpi -e 's/i=\$\{BEGIN\}/i=\$\{BEGIN\}-1/'

Perl one-liner to remove trailing space from a file

I am calling a perl one-liner inside a Perl script.
The intention of the one-liner is to remove the trailing space from a file.
Inside the main perl script:
`perl -pi -e 's/\s+$//' tape.txt`;
Now it is throwing me an error Substitution replacement not terminated at -e line 2.
Where is it going wrong?
It's because of the $/ (special variable) inside your main perl script. Note that variables are interpolated inside `` strings just like inside "" strings, and the fact that there are some single quotes in there doesn't change that. You need to escape that $:
`perl -pi -e 's/\s+\$//' tape.txt;`
The backtick syntax invokes a shell and when invoked, the shell assumes it should interpolate the string passed.
A cleaner syntax might be:
system('perl -pli -e "s/\s*$//" tape.txt');
Since you aren't capturing the output of the command, using backticks or qx in lieu of system isn't an issue.
Too, adding the -l switch autochomps each line read and then adds a newline back --- probably what you want.
\s matches [ \t\n\r\f] and do not want to match \n.
Notice use of {} for subst delimiters:
$ echo -e 'hi \nbye'| perl -pe 's{[\t\040]+$}{};' | cat -A
hi$
bye$