Issue
I have below code in perl file. The intention of this command is to add a line "- - test 0" at the end of file.txt.
system(" find . -type f -name file.txt | xargs sed -i -e "$ a- - test 0" ");
When i try to run the script, I'm getting error as shown below.
Scalar found where operator expected at timeStampConfig.pl line 24, near "" find . -type f -name file.txt | xargs sed -i -e "$ a"
(Missing operator before $ a?)
Number found where operator expected at timeStampConfig.pl line 24, near "test 0"
(Do you need to predeclare test?)
String found where operator expected at timeStampConfig.pl line 24, near "0" ""
(Missing operator before " "?)
syntax error at timeStampConfig.pl line 24, near "" find . -type f -name file.txt | xargs sed -i -e "$ a"
Execution of timeStampConfig.pl aborted due to compilation errors.
I tried executing below line from the command prompt and it works fine.
find . -type f -name file.txt | xargs sed -i -e '$ a- - test 0'
I also tried to use single quotes as shown below but ended up with an error.
system("find . -type f -name file.txt | xargs sed -i -e '$ a- - test 0'");
sed: -e expression #1, char 1: unknown command: `-'
I'm new to perl, need some help with this.
Solution
When you want double quotes within a double-quoted string, you need to escape the double-quotes: "...\"foo\"..."
but in this case you should most probably replace the inner double-quotes with single-quotes:
system("find . -type f -name file.txt | xargs sed -i -e '\$ a- - test 0'");
# ^ ^
# +---- here ----+
Note that the $
needs to be escaped in the double-quoted string. You could also separate the building of the string to avoid having to escape anything:
my $expr='$ a- - test 0';
system("find . -type f -name file.txt | xargs sed -i -e '$expr'");
Answered By - Ted Lyngmo Answer Checked By - Dawn Plyler (WPSolving Volunteer)