Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

multi-line in-place edits

by Tuppence (Pilgrim)
on Feb 15, 2005 at 18:54 UTC ( #431292=perlquestion: print w/replies, xml ) Need Help??
Tuppence has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

Having already replaced every \n in my source tree with \266 due to a bad command line, I need help with what I expected to be very simple.

I'm trying to run a multi-line search replace against a file from the command line.

Simplified, I want

perl -pi -e 's/(.*)/{\1}/gs' filename.txt

to put a { } wrapper around the entire file. Instead, it puts a { } wrapper around each line. (and some weird extra ones, but eh whatever)

I thought this could be solved with something like -l0666, but I can't find why I think that and it already toasted my code tree once...


Replies are listed 'Best First'.
Re: multi-line in-place edits
by cog (Parson) on Feb 15, 2005 at 19:05 UTC
    This works:

    perl -pi -e 'BEGIN{undef$/};s/(.*)/<$1>/s' filename.txt

    A couple of notes:

    • Use $1 instead of \1 (use \1 only in the left part of the substitution
    • The /g switch fell off (you only wanted to do it once)

    And... I guess that's it... I undefined $/ so that the file would be read all at once :-)


Re: multi-line in-place edits
by bmann (Priest) on Feb 15, 2005 at 19:33 UTC
    I thought this could be solved with something like -l0666,

    Take a look at perlrun, -0777 will slurp the entire file. Here's a working one-liner: perl -i.bak -0777 -ne 'print "{$_}"' filename
    Good luck!

Re: multi-line in-place edits
by Roy Johnson (Monsignor) on Feb 15, 2005 at 19:38 UTC
    perl -n0777i.bak -e 'print "{$_}"' filename.txt
    The 0777 option does the same thing as the BEGIN that others are suggesting. See perldoc perlrun.

    Caution: Contents may have been coded under pressure.
Re: multi-line in-place edits
by holli (Monsignor) on Feb 15, 2005 at 19:36 UTC
    perl -0ni -e "print qq({$_})" file

    holli, /regexed monk/
      perl -0ni -e "print qq({$_})" file

      Caveat - this will fail quietly if the file contains any null bytes. -0 with no arguments sets the record separator to \0.

      Better to explicitly use -0777. This will cause perl to slurp the whole file, since "there is no legal byte with that value" (quoted from perlrun)

Re: multi-line in-place edits
by sh1tn (Priest) on Feb 15, 2005 at 19:00 UTC
    perl -i -pe 's/(.+)/{$1}/' filename.txt

    Update: return of deleted errors ...
      Yours wraps each line - I want the whole file wrapped. Although yours does get rid of the funny extra sets...
        Yes, I read your post more carefully and delete my answer.
Re: multi-line in-place edits
by halley (Prior) on Feb 15, 2005 at 19:23 UTC
    I am not sure I understand the value of your task (or what you meant by adding \266 bytes). Are you trying to undo a command-line mistake? Are you trying to use some tool that avoids working inside braces, so you're wrapping the file in braces? It all sounds odd. This doesn't work,
    perl -p -i -e 'BEGIN { print "{" } END { print "}" }' filename.txt
    but this does. Not as quick to type, but...
    echo "{" >tmp cat filename.txt >>tmp echo "}" >>tmp mv tmp filename.txt

    [ e d @ h a l l e y . c c ]

      I provided a simplified example to get the multi-line edit syntax worked out.

      I was attempting to do some code refactoring with in-place editing, when due to failing memory I replaced all the \n's with a upper ascii char. I managed to get that fixed, and just needed the correct syntax for multi line edits.

      -l0666 is so close to the -0777 that I really needed I think perl should have just known I was doing the wrong thing and Done the Right Thing ;) /joke

Re: multi-line in-place edits
by sh1tn (Priest) on Feb 15, 2005 at 20:43 UTC
    perl -pi -e '1 == $. and s/^/{/; eof and s/$/}/' file.txt

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://431292]
Approved by jfroebe
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2017-05-29 07:07 GMT
Find Nodes?
    Voting Booth?