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

Hello All , I have one Php script that will fetch some pattern from all the files of particular directory. I have saved the matched result in result.txt . that file contains one extra line which i need to remove The single line , to be removed is
(.*)$/',$lines,$matches) 10.(.*)$/',$lines,$matches) (.*)$/ +',$lines,$matches)
I have used , the below perl script to do so, But gives me error = > Unmatched '.
perl -pi -e 's#\Q(.*)\$/',\$lines,\$matches))10.(.*)\$/',\$lines,\$mat +ches))(.*)\$/',\$lines,\$matches))# #gi' result.txt > result1.txt
Please , help me out. Thanks,

Replies are listed 'Best First'.
Re: Delete a line from the file
by NetWallah (Canon) on Jul 16, 2008 at 22:04 UTC
    Looks like your shell (bash ?) is reporting the error.

    Since you are passing a string to perl that is supposed to be enclosed in single-quotes, you need to use SHELL-ESCAPE characters if you want to use single-quote inside that string. Or, use double-quotes.

    Shell-escape is usually a backslash, similar to perl, so you need to b aware as to which backslash gets eaten by the shell, and which one is for perl's consumption.

    Update: Ignore this - While the diagnosis is correct, the proposed solution is wrong. Better information in my response below...

         Have you been high today? I see the nuns are gay! My brother yelled to me...I love you inside Ed - Benny Lava, by Buffalax

      Hi, Thanks for the Help ,yes,It's output from shell. I've tried using shell_escape, as below,
      perl -pi -e shell_escape('s#\Q(.*)\$/\',\$lines,\$matches))10.(.*)\$/ +\',\$lines,\$matches))(.*)\$/\',\$lines,\$matches))# #gi') result.tx +t > result1.txt
      but got the same erroe .. Unmatched '. Whether I have used the shell_escape correctly or not.. Thanks,
        \' doesn't work in single-quoted literals in bourne shell.
        $ echo 'foo\'bar' > <- waits for closing '

        Fixes:

        $ echo foo\'bar foo'bar $ echo "foo'bar" foo'bar $ echo 'foo'\''bar' foo'bar $ echo 'foo'"'"'bar' foo'bar
        From the bash manual:
        A single quote may not occur between single quotes, even when preceded by a backslash.
        Hence - my previous suggestion is invalid - there is no "escaping" inside a single-quote.

        Here is a shell trick to try:

        '"'"'
        Replace all perl-directed single quotes with that, and it should work.

        Extremely ugly, though. Here is how it works:
        It starts in single quotes, and which you want a single quote you come out, go into double quotation to quote a single quote, then come out of double quotation, to go back into single quotes again.

        Using this trick, your command passes the shell's syntax:

        perl -pi -e 's#\Q(.*)\$/'"'"',\$lines,\$matches))10.(.*)\$/'"'"',\$li +nes,\$matches))(.*)\$/'"'"',\$lines,\$matches))# #gi' result.txt > re +sult1.txt -- Output (from perl - bash is happy!!!) -- Can't open result.txt: No such file or directory.
        Update: I just noticed that my shell trick is identical to ikegami's (++) last example.

             Have you been high today? I see the nuns are gay! My brother yelled to me...I love you inside Ed - Benny Lava, by Buffalax

Re: Delete a line from the file
by cdarke (Prior) on Jul 17, 2008 at 08:23 UTC
    Surprising that no one mentioned a here document. Get perl to read a script from stdin using a script name of - (hyphen). For example:
    perl -n - gash.txt << END # insert fun Perl here print qq(Quote ': double: "); END
    You can also quote the label, which quotes the data in the heredoc. Alternatively, ditch the shell and use Perl for everything.