I am using this command to delete multiple occurrences of a line in my script and it just works fine when I run it on the command prompt. However when I use this line in my perl script it throws an error SED: syntax error at script.pl line 19, near ";$'". Below is the line in my script. I tried escaping $ but that dint help

sed '/<\/data_item>/{N;/<\/data_item>$/{N;$'!'{s/\n//;D}}}' $WX;

  • sed is not part of Perl. But I'm going to give you the benefit of the doubt and assume you've declared your own subroutine called sed elsewhere in your script. Which command prompt are you saying this worked with ? I just tried it in bash and it gives an error. I must say I'm confused by the exclamation mark ! half way through. Perhaps you could give the original "command prompt" version and explain what you are trying to do. – Lqueryvg May 9 '14 at 19:08
  • Sorry I am using backticks here. I have other simpler sed commands in my perl script and they just work fine..In the above script perl points to soemthing around this part $/{N;$'!'{s/\n//;D}}}' and throws an error – user3614491 May 9 '14 at 19:11
  • Let's see one of these "sed commands" from another part of your Perl script which work fine. – Lqueryvg May 9 '14 at 19:16

OK, giving you the benefit of the doubt (that you've defined your own subroutine called sed), your "Perl" statement breaks down as follows:

sed                                           # A call to a subroutine called "sed"
                                              # that you've defined ?

'/<\/data_item>/{N;/<\/data_item>$/{N;$'   # A string surrounded by single quotes

!                        # an exclamation mark

'{s/\n//;D}}}'           # another string surrounded by single quotes

$WX;                     # a scalar variable called "WX"

The actual syntax error you are getting is because of the exclamation mark. In Perl you can't just put characters outside of strings and have them concatenate with the surrounding strings like you can in shell.

Even in shell, I'm not sure if the line as you've quoted it would work. So I think you'd really need to expand on your original question to progress this.

  • Actually on the shell it works only with single quotes around $'!' otherwise it throws someother error. Other examples in perl script aresed -i '2,\${/xml version/d;}' $WX ; sed -i '3,\${/top/{N;d;}}' $WX ; sed -i '3s/=.*\$/="check">/' $WX; and they all just work fine.. – user3614491 May 9 '14 at 20:51
  • OK, makes more sense now I can see the backticks. Looks like you're going to need to escape a few characters here or there. Please provide exact command that worked from the command line. I'm still confused by that exclamation mark. – Lqueryvg May 9 '14 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.