Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

inline file modification

by Woodchuck (Initiate)
on Sep 07, 2005 at 16:07 UTC ( #489913=perlquestion: print w/replies, xml ) Need Help??
Woodchuck has asked for the wisdom of the Perl Monks concerning the following question:


simple file read and write question. I've looked it up on the database but could not find what I wanted (or is it that I don't know how or what to look for)... ok... cutting to the chase...

I'm reading a file and when I find a certain line which contains some 'idTag' character I wish to overwrite it. The fact is the lines don't always have the same length. Somtimes... they're longer... sometimes shorter...
Is it possible to do this in a clean way with the read and write operator "+<" without having to introduce a temporary file?

Speaking of which... I can't really seem to find a use for the tmp_file constructor in the IO::File module. If you have an example please let me know.

Thanks in advance!

-Would choke

p.s. I hate it because the more I code, the messier it gets...

2005-09-07 Retitled by Corion, as per Monastery guidelines
Original title: 'Unworthy me!'

Replies are listed 'Best First'.
Re: inline file modification
by sk (Curate) on Sep 07, 2005 at 16:25 UTC
    This code block will do an inplace edit for the file mydata.txt. $^I allows you to specify the backup file extension.

    You can turn off backup feature but it is dangerous or rather not a good idea.

    code below is untested

    { local $^I = ".bak"; local *ARGV; @ARGV = "mydata.txt"; while (<>) { s/idTag/newTag/g; print; } }
      sounds interesting thank you.
Re: inline file modification
by joeface (Pilgrim) on Sep 07, 2005 at 17:50 UTC

    Look at the Tie::File module. It allows you to edit a file in-place as if it were an array.

    This example replaces any line containing $idtag with a line containing $replacement.

    use Tie::File; my $file = 'filename'; my @tied_file; my $idtag = 'idTag'; my $replacement = 'replacement line'; # Create the array @tied_file from $file tie(@tied_file, 'Tie::File', $file); # Preserve the index so we can work directly on lines of @tied_file foreach my $i (0 .. $#tied_file) { if ($tied_file[$i] =~ /$idtag/) { # Do something to this line of @tied_file $tied_file[$i] = $replacement; } } untie(@tied_file);
      Thank you, this is just the bit I needed. I suggest you put this in the snippets barn,as it might prove to be useful to a lot of people.
      keyword-> read write file
      Thanks again!

Re: inline file modification
by Codon (Friar) on Sep 07, 2005 at 16:37 UTC
    Is this something that needs to happen within a greater context, or is this something that can be simply done with
    # untested substitution perl -pi -e 's/^.+idTag.+$/new data here/' file.txt

    Ivan Heffner
    Sr. Software Engineer, DAS Lead, Inc.
      first option

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://489913]
Approved by BaldPenguin
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2016-08-30 22:44 GMT
Find Nodes?
    Voting Booth?
    The best thing I ever won in a lottery was:

    Results (424 votes). Check out past polls.