Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

In-place header/footer

by swiftone (Curate)
on Apr 01, 2002 at 15:38 UTC ( [id://155773]=perlquestion: print w/replies, xml ) Need Help??

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

I want to take all files in a directory tree and slap some text to the start and end of wach file.

Now rather than breaking File::Find out, I figured this was an easy one-liner. I don't do many one-liners, but I opened up perlrun, read (I thought) the relevant sections, and ran this from the root of a copy of the directory tree:  perl -i -e'print "prepend\n"; print join "", <>; print "append\n";' *.html */*.html */*/*.html */*/*/*.html This prints everything to the screen, and turns everything into 0 byte files.

Any ideas what I'm missing?

Replies are listed 'Best First'.
Re: In-place header/footer
by perrin (Chancellor) on Apr 01, 2002 at 16:43 UTC
    Here's how I would do it:

    perl -pi.bak -0 -e 's/(.*)/prepend\n$1append\n/s' *.html

    This works by using the -p switch to set up a loop around the contents of each file and the -0 switch to set the end of line terminator ($/) to null. Then you can peform a regex on the entire file as one big string.

      Works beautifully! Many thanks.

      (for the curious, so did RMGir's answer above)

Re: In-place header/footer
by RMGir (Prior) on Apr 01, 2002 at 16:21 UTC
    (Edit: See perrin's answer; his way is MUCH cleaner)

    The problem is your slurp of all the lines in your join, I think. -i only works right if it can match up your print to the file currently being read, and your <> slurped up all the files.

    I _think_ this may work better for you (untested!):

    perl -i.bak -ne'if($f ne $ARGV[0]){if(defined($f)){print "prepend\n",@ +lines,"append\n"; @lines=();} $f=$ARGV[0]} push @lines,$_;' *.html */ +*.html */*/*.html */*/*/*.html
    It's alway safer to use .bak on your -i, in case of disasters like the one you mentioned in your post.

    If it all worked, it's easy to get rid of the .bak files.

    You might also want to think about using find | xargs instead of the nested directory wildcards; it's probably going to work more efficiently, and it won't croak on a huge directory tree.
    --
    Mike

Re: In-place header/footer
by Fletch (Bishop) on Apr 01, 2002 at 19:01 UTC

    Not that this hasn't already been directly answered, but a possibly tangentally useful module to check out might be Apache::Sandwich if you're using mod_perl.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://155773]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-23 15:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found