rancho_trojan has asked for the wisdom of the Perl Monks concerning the following question:

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;

Replies are listed 'Best First'.
Re: sed in perl: syntax error at script.pl line 19, near “;$'”
by kcott (Archbishop) on May 10, 2014 at 09:27 UTC

    G'day rancho_trojan,

    Welcome to the monastery.

    There are multiple ways to run an external command from Perl: you need to show us exactly what code you used.

    It sounds like you used some interpolating quoting mechanism (e.g. qx{sed ...}). If so, Perl will evaluate parts of the quoted string as variables (the ones I've spotted: $/, $' and $WX) and escape sequences (again, the ones I've spotted: \/ [twice] and \n). So, even when you eradicate all of Perl's syntax errors, you may just end up sending complete garbage to sed.

    What you might be able to do (and I provide no guarantee this will work) is start with code like this:

    print qq{sed ...};

    Keep editing the ... part until the command you want is printed; then change print qq back to just qx and see if does what you want. Of course, that assumes you're using qx{}; although, you could probably do something similar for other scenarios.

    From a Perl script, you'd probably be far better off using perl, rather than sed, to remove lines from a file. There are potentially many ways to do this: until we know about the data you're processing and in what context this processing occurs, we can't really advise you further on that.

    Note that saying "I tried escaping $ but that dint help" does doesn't help us a whole lot. We don't know if you tried escaping one, some or all of the $ characters. We also don't know in what way, whatever you did, didn't help.

    The guidelines in "How do I post a question effectively?" will help you formulate a question which we can answer immediately. Having to request formatting such that we can read your question [I didn't see the original], and then having to request all sorts of other information such that we can answer your question, doesn't really help anyone.

    Update: s/does/doesn't/ (2nd to last paragraph). Thanks to Lotus1 for spotting this.

    -- Ken

Re: sed in perl: syntax error at script.pl line 19, near “;$'”
by choroba (Cardinal) on May 09, 2014 at 21:31 UTC
    Crossposted at SuperUser.com and AskUbuntu.com. It is considered polite to inform about crossposting, so people not attending all sites don't waste their time hacking a problem already solved at the other end of the Internet.

    Also, please use <code> ... </code> tags to format your code sample.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Though it is a cross posting, it has not been answered anywhere and it's still something I am looking for
        Hum, I naively thought that I had given you an answer a quarter of an hour before your last post above.
Re: sed in perl: syntax error at script.pl line 19, near “;$'”
by Laurent_R (Canon) on May 09, 2014 at 23:03 UTC
    Well, sed is a nice utility, but it is not part of Perl syntax. Other monks have mentioned that your formating is messy and makes it difficult to understand your post. You probably want to use the Perl s/// command rather than the Unix sed command.
      Ok I just added code formatting as mentioned.

        Hi rancho trojan

        Ok I just added code formatting as mentioned.
        That is good, but also did you see this statement
        ..sed is a nice utility, but it is not part of Perl syntax.. from Laurent_R?

        So for you to make your code work without any change, since it worked for you on the CLI, please check the usage of system in perl.
        You might really start thinking of doing all your work in perl like it was previously advised, instead of calling sed in perl, as you will like to do.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
Re: sed in perl: syntax error at script.pl line 19, near “;$'”
by wjw (Priest) on May 09, 2014 at 21:30 UTC
    You will get a better answer if you just post your code in code tags and do a bit of formatting...

    Markup. Give that a try first....

    ...the majority is always wrong, and always the last to know about it...
    Insanity: Doing the same thing over and over again and expecting different results...
Re: sed in perl: syntax error at script.pl line 19, near “;$'”
by locked_user sundialsvc4 (Abbot) on May 10, 2014 at 11:28 UTC

    All that you can realistically do here is to fiddle-with sed ... but you probably could have the job done right now already with a text editor.   Just sayin’.   The trouble is that sed is not a true parser, and such a thing is probably what you do need here.   It has no language awareness.   Therefore the slightest discrepancy between what your “one-liner” calls for and what the source-code contains ... anywhere ... is going to cause problems.   Long before this point, if I may be quite frank, I would have cut my losses and found a different way to do it.   “There’s More Than One Way To Do It™” very often means that the approach that you have first come up with isn’t the right way ... or even a way that will work at all.

    In this case, you have to put your thinking-cap back on.   You seem to have wasted many days trying to do something in a few seconds when it would have taken minutes to do it by hand.   The “shell-script one-liner,” in all its various forms, is not only highly-overrated but virtually unmaintainable over time.   If what you truly need is to generate customized “Perl code,” well, that is simply a special-case of ... templating.   Which we already know how to do very well.   Template ... the same tool that generates so many of the world’s web pages now ... can quickly generate whatever you might need for it to do.

      The “shell-script one-liner,” in all its various forms, is not only highly-overrated but virtually unmaintainable over time.

      I disagree with this, although I broadly agree with the rest of what you said. I am using very commonly "one-liners", Perl or sed or awk or grep or cut or other (mostly Perl), to pre-process big files and get the right input format for my real programs. One-liners can be a bless of God.