Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

modifying and overwriting a file

by mndoci (Scribe)
on May 17, 2001 at 08:59 UTC ( [id://81165]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Folks,

Here is a question for which I have not quite found the most efficient solution in the books at my disposal. What I want to do is read in a file at the command line and add an underscore to each line in the file. As an advanced case I would like to do this for every line except the first. I can print to STDOUT using the following code
while (<>){ chomp $_; $newline = $_ . "_"; print $newline; print "\n"; }

This prints out each line to STDOUT. I can redirect the output to a newfile, but what I want to do is overwrite the original file. What would be the most efficient way to do this? Of course I have the more advanced case where I do not want to touch line #1. As far as I can figure out I need a way to bybass $_[0] and only modify after that but am not quite sure how to do it

Thanks

mndoci

Replies are listed 'Best First'.
Re: modifying and overwriting a file
by chipmunk (Parson) on May 17, 2001 at 09:16 UTC
    I like to take advantage of perl's command-line switches, so I'd do something like this:
    #!perl -pli $_ .= '_' unless $. == 1; # append underscore, except on first line close ARGV if eof; # reset line numbering at end of file
    -p loops over the input, reading it into $_ and printing it back out. -l handles line-endings. -i modifies files in place.
Re: modifying and overwriting a file
by jepri (Parson) on May 17, 2001 at 09:08 UTC
    If by 'efficient' you mean 'a one liner' then this is what you are looking for:

    perl -pe 'print "_" if $a++;' -i.bak yourfile.txt

    Lookup perlrun for more detail on what's going on there.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

Re: modifying and overwriting a file
by bjelli (Pilgrim) on May 17, 2001 at 12:16 UTC

    In the perl man-pages "modifying and overwriting a file" is called inplace-editing. Perl has special provisions for that.

    chipmonk explained the usage on the command line in his previous article. If you don't want to use the command line you can use some built-in variables. Quoting from perlvar:

    $INPLACE_EDIT
    $^I

    The current value of the inplace-edit extension. Use undef to disable inplace editing. (Mnemonic: value of -i switch.)

    @ARGV

    The array @ARGV contains the command line arguments intended for the script....

    $ARGV

    contains the name of the current file when reading from <>.

    to inplace-edit several file, you push them onto @ARGV, set $^I if you want to have backups of the files, and then just read from <> and write to STDOUT like so:

    @ARGV = ('firstfile.txt', 'secondfile.txt'); $^I = '.bak'; while (<>){ # print STDERR "working on file $ARGV\n"; chomp $_; print $_, "_\n"; }

    This creates backups of the original files as a side effect (the backups are called 'firstfile.txt.bak' and 'secondfile.txt.bak')

    BEWARE! If you run the program again, you add another underscore, and loose the backup. Avoiding that is left as an exercise to the reader :-)

    --
    Brigitte    'I never met a chocolate I didnt like'    Jellinek
    http://www.horus.com/~bjelli/         http://perlwelt.horus.at

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2024-04-19 15:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found