Replace a string in file with new string containing $ sign in Perl - perl

I am trying to replace string with a new string containing $(dollar) sign in a existing file.
I have used below (regex) line in my code but I am not able get the expected result. Please suggest the solution.
system("perl -pi -e \"s/length\\s+.*/length [index $pkt_len]/g if /^\\s*set_value\\s+length/\" $filename")

The way to find the solution is during debug you should print out the line of the system call and test it "by hand" if it works as expected in the shell.
My guess is that
system("perl -pi -e 's/length\\s+.*/length [index \\\$pkt_len]/g if /^\\s*set_value\\s+length/' $filename")
is a solution.

You need 7 backslashes (!):
system("echo a | perl -pe \"s/a/\\\\\\\$foo/\"");
Output:
$foo
First "echo a | perl -pe \"s/a/\\\\\\\$foo/\"" is interpreted by perl which reduces the string to echo a | perl -pe "s/a/\\\$foo/"
Then it is parsed by the shell sh to give : echo a | perl -pe s/a/\$foo/
Finally, it is parsed by perl a second time to give the output $foo.

Related

Search for string in text file and print line until next tab \t

I have a fairly complex text file file1.txt that hasn't been munged properly. The file is tab-delimited however, i.e. each string is separated by \t.
I would like to write a script/use a Unix command that parses this entire file for a certain string string1: which will print the line after the colon until stopping at \t.
The text file looks like this:
...kjdafhldkhlfak\tSTRING1:Iwanttokeepthis\tfadfasdafldafh\tSTRING1:andthis\tafsdkfasldh....
So the grep like function outputs
Iwanttokeepthis
andthis
In Perl, I know how to print a string if it occurs with
perl -wln -e 'print if /\bSTRING1\b/' file1.txt
How would one revise this to print the line between STRING1: and \t?
With Perl:
$ echo $'kjdafhldkhlfak\tSTRING1:Iwanttokeepthis\tfadfasdafldafh\tSTRING1:andthis\tafsdkfasldh' > /tmp/file
perl -lne 'while (/STRING1:([^\t]+)\t/g) {print $1}' /tmp/file
Iwanttokeepthis
andthis
Or, as stated in comments:
$ perl -nle'print for /STRING1:([^\t]*)\t/g' /tmp/file
Iwanttokeepthis
andthis
With GNU grep:
grep -Po 'STRING1:\K.*?(?=\t)' file
Output:
Iwanttokeepthis
andthis
See: The Stack Overflow Regular Expressions FAQ

perl one-liner to keep only desired lines

I have a text file (input.txt) like this:
NP_414685.4: 15-26, 131-138, 441-465
NP_418580.2: 493-500
NP_418780.2: 36-48, 44-66
NP_418345.2:
NP_418473.3: 1-19, 567-1093
NP_418398.2:
I want a perl one-liner that keeps only those lines in file where ":" is followed by number range (that means, here, the lines containing "NP_418345.2:" and "NP_418398.2:" get deleted). For this I have tried:
perl -ni -e "print unless /: \d/" -pi.bak input.txt del input.txt.bak
But it shows exactly same output as the input file.
What will be the exact pattern that I can match here?
Thanks
First, print unless means print if not -- opposite to what you want.
More to the point, it doesn't make sense using both -n and -p, and when you do -p overrides the other. While both of them open the input file(s) and set up the loop over lines, -p also prints $_ for every iteration. So with it you are reprinting every line. See perlrun.
Finally, you seem to be deleting the .bak file ... ? Then don't make it. Use just -i
Altogether
perl -i -ne 'print if /:\s*\d+\s*-\s*\d+/' input.txt
If you do want to keep the backup file use -i.bak instead of -i
You can see the code equivalent to a one-liner with particular options with B::Deparse (via O module)
Try: perl -MO=Deparse -ne 1 and perl -MO=Deparse -pe 1
This way:
perl -i.bak -ne 'print if /:\s+\d+-\d/' input.txt
This:
perl -ne 'print if /:\s*(\d+\s*-\s*\d+\s*,?\s*)+\s*$/' input.txt
Prints:
NP_414685.4: 15-26, 131-138, 441-465
NP_418580.2: 493-500
NP_418780.2: 36-48, 44-66
NP_418473.3: 1-19, 567-1093
I'm not sure if you want to match lines that are possibly like this:
NP_418580.2: 493-500, asdf
or this:
NP_418580.2: asdf
This answer will not print these lines, if given to it.

Perl command line search and replace with multiple expressions

I am using Perl to search and replace multiple regular expressions:
When I execute the following command, I get an error:
prompt> find "*.cpp" | xargs perl -i -pe 's/##(\W)/\1/g' -pe 's/(\W)##/\1/g'
syntax error at -e line 2, near "s/(\W)##/\1/g"
Execution of -e aborted due to compilation errors.
xargs: perl: exited with status 255; aborting
Having multiple -e is valid in Perl, then why is this not working? Is there a solution to this?
Several -e's are allowed.
You are missing the ';'
find "*.cpp" | xargs perl -i -pe 's/##(\W)/\1/g;' -pe 's/(\W)##/\1/g;'
Perl statements has to end with ;.
Final statement in a block doesn't need a terminating semicolon.
So a single -e without ; will work, but you will have to add ; when you have multiple -e statements.
Having multiple -e values are valid, but is it useful? The values from the multiple -e are merely combined into one program, and it's up to you to ensure that together they make a syntactically correct program. The B::Deparse program can show you what perl thinks the program is:
$ perl -MO=Deparse -e 'print' -e 'q(Hello' -e ')'
print "Hello\n";
-e syntax OK
A curious thing to note is that a newline snuck in there. Think about how it got there to see what else perl is doing to combine multiple -e values.
In your program, you are substituting on the current line, then taking the modified line and substituting again. That's better written as:
prompt> find "*.cpp" | xargs perl -i -pe 's/##(\W)/\1/g; s/(\W)##/\1/g'
Now, if you are building up this command line by adding more and more -e through some automated process and you don't know ahead of time what you get, maybe those -e make sense. However, you might consider that you can do the same thing to build up the string you give to -e. I don't know what might be better because you didn't explain why you are doing it that way.
But, I suspect that in some cases, people are actually thinking about having only one substitution work. They want to try one and if its pattern doesn't work, try a different one until one succeeds. In that case you don't want to separate the substitutions by semicolons. Use the short-circuiting || instead. The s/// returns the number of substitutions it made and || will stop (short circuit) when it finds a true value:
prompt> find "*.cpp" | xargs perl -i -pe 's/##(\W)/\1/g || s/(\W)##/\1/g'
And note, you only need one -p. It only does its job once. Here's the program with multiple -p deparsed:
$ perl -MO=Deparse -i -pe 's/##(\W)/\1/g;' -pe 's/(\W)##/\1/g;'
BEGIN { $^I = ""; }
LINE: while (defined($_ = readline ARGV)) {
s/##(\W)/$1/g;
s/(\W)##/$1/g;
}
continue {
die "-p destination: $!\n" unless print $_;
}
-e syntax OK
It's the same thing as having only one -p:
$ perl -MO=Deparse -pi -e 's/##(\W)/\1/g;' -e 's/(\W)##/\1/g;'
BEGIN { $^I = ""; }
LINE: while (defined($_ = readline ARGV)) {
s/##(\W)/$1/g;
s/(\W)##/$1/g;
}
continue {
die "-p destination: $!\n" unless print $_;
}
-e syntax OK
Thanks so much! You helped me reduce my ascii / decimal / 8-bit binary table printer enough to fit in a tweet:
for i in {32..126};do printf "'\x$(printf %x $i)'(%3i) = " $i; printf '%03o\n' $i | perl \
-pe 's#0#000#g;' -pe 's#1#001#g;' -pe 's#2#010#g;' -pe 's#3#011#g;' \
-pe 's#4#100#g;' -pe 's#5#101#g;' -pe 's#6#110#g;' -pe 's#7#111#g' ; done | \
perl -pe 's#= 0#= #'

Perl regex to act on a file from the command line

In a file, say xyz.txt i want to replace the pattern of any number followed by a dot example:1.,2.,10.,11. etc.. with a whitespace.
How to compose a perl command on the command line to act on the file to do the above, what should be the regex to be used ?
Please Help
Thank You.
This HAS to be a Perl oneliner?
perl -i -pe 's/\d+\./ /g' <fileName>
The Perl command line options: -i is used to specify what happens to the input file. If you don't give it a file extension, the original file is lost and is replaced by the Perl munged output. For example, if I had this:
perl -i.bak -pe 's/\d+\./ /g' <fileName>
The original file would be stored with a .bak suffix and <fileName> itself would contain your output.
The -p means to enclose your Perl program in a print loop that looks SOMEWHAT like this:
while ($_ = <>) {
<Your Perl one liner>
print "$_";
}
This is a somewhat simplified explanation what's going on. You can see the actual perl loop by doing a perldoc perlrun from the command line. The main idea is that it allows you to act on each line of a file just like sed or awk.
The -e simply contains your Perl command.
You can also do file redirection too:
perl -pe 's/\d+\./ /g' < xyz.txt > xyz.txt.out
Answer (not tested):
perl -ipe "s/\d+\./ /g" xyz.txt
Both
perl -ipe "s/\d+\./ /g" xyz.txt
and
perl -pie
cannot execute on my system.
I use the following order:
perl -i -pe

Why does Perl and /bin/sha1 give different results?

I'm confused as to why the following return separate sHA1s
$ perl -MDigest::SHA1 -E'say Digest::SHA1::sha1_hex("http://i.aultec.com/v/8066/Originals/1FTVX12585NA9832010.jpg");'
e1133fa3b7ea0bfb8ffa4d877932ed6c6fa10cef
$ echo "http://i.aultec.com/v/8066/Originals/1FTVX12585NA9832010.jpg" | sha1sum
5c3731e83ae0184ed93b595b9f5604863dd331e6 -
Which one is right? Am /I/ doing it wrong?
$ perl -MDigest::SHA -E'say Digest::SHA::sha1_hex("http://i.aultec.com/v/8066/Originals/1FTVX12585NA9832010.jpg");'
e1133fa3b7ea0bfb8ffa4d877932ed6c6fa10cef
You can see the digest is right in the successor (Digest::SHA)
Both are right. Your echo command includes a newline at the end. (and the perl string doesn't) Try with echo -n ...
Perl is giving you the hash of the literal string you entered, whereas echo is appending a newline. If you tell echo to not add a newline, you'll get the same result:
drewfus:~$ perl -MDigest::SHA1 -E'say Digest::SHA1::sha1_hex("foo");'
0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
drewfus:~$ echo -n "foo" | sha1sum
0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33 -
This is such a frequent mistake and I've made it many times. The echo command is also returning a newline.