Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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:

Hello,

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!'

Comment on inline file modification
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 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
    WhitePages.com, Inc.
      first option
        This node was taken out by the NodeReaper on Wed Sep 7 19:14:59 2005 GMT
        Reason: [ww]: delete. empty node

        For more information on this node visit: this

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!

      -Chuck

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2014-11-26 13:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (171 votes), past polls