I have the following two commands :
value=`grep -o Logs\/.*txt\" textFILE`
perl -i -wpe's/" onclick="img=document\.getElementById\('\''img_1'\''\); img\.style\.display = \(img\.style\.display == '\''none'\'' \? '\''block'\'' : '\''none'\''\);return false"/$value/' textFILE
However, I keep on getting a Use of uninitialized value $value in concatenation (.) or string at -e line 1, <> line 56.
error. Can someone tell me what I am doing wrong.
For an example like the following:
When we run Perl to execute our code here we are not sending the contents of the shell variable $value to Perl. We are sending the literal string '$value', which Perl will then try and print the Perl variable $value, which does not exist.
Here are two ways that you could correct this.
The first would be to wrap your Perl code in double quotes instead of single quotes so that $value becomes the contents of the shell variable $value instead of the literal string '$value' when sent to Perl:
However when using this method if you have Perl variables along with this shell variable you will need to 'escape' the Perl variables or you'll get an error like this:
Using \'s will correct this:
Secondarily, you can avoid changing the single quotes to double quotes by exporting "value" to the environment, and then accessing it through the %ENV hash already available inside your Perl program (http://perldoc.perl.org/Env.html). For example:
Be mindful of your environment that you do not accidentally overwrite some needed environment variable. Using a naming convention very specific to your work can help:
printenv
on most Linux systems will show you the current established environment variables. You could also just use Perl to inspect %ENV.Putting together your two earlier questions,
Why is my perl replace not working?
andHow do I get a selection from the output of a grep
, I will once again advise you to just do it all in perl.The following script accepts a filename as a parameter and reads it for the first value, and then edits it in place to replace the second value with the first: