Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


How do I modify or insert a line in a file? by MikeLacey
Posted: 15 Mar 01 (Edited 11 Jun 04)

FAQ compiled and maintained by MikeLacey, contributions from yolond and stefanos. Please browse through FAQ219-2884 and FAQ219-2889 first. Comments on this FAQ and the General FAQ's are very welcome.

This isn't as easy as it sounds actually. There's no easy way to change just the one line of a variable record length text file, which most text files are. There's no way, at all, to insert data into the middle of a file.

What you have to do is read the file into memory (an array probably), close the file, make your changes, open the file for writing and write the whole thing back.

As long as the file isn't too big (too easily fit in memory) that approach works OK.

If the file *is* too big you have to:

Read the file, line by line, and write it out to a second file - complete with your changes, as you go. When you're done, delete the first file and rename the file you just created to have the same name as the first file.

yolond posted this code and I fiddled with it a bit, it's based upon the first approach above.


$input = "users.txt"
$id = 'line I want to edit';

open(INPUT, &quot;<$users&quot;);  # open the file to read it
@data = <INPUT>;         # read the whole file into an array
close(INPUT);            # close the file

open (OUTPUT, &quot;>$users&quot;); # now open it to write to
flock(OUTPUT, 2);         # and lock it

for ($i=0;$i<$#data+1;$i++)    {         # for each line in the array
    chomp($data[$i]);                    # get rid of any trailing new-line character
    if ( $id eq $data[$i] )    {         # if it matches the data I'l looking for
        $data[$i] = &quot;new data&quot;;          # replace it
        print OUTPUT &quot;$data[$i]\n&quot;;      # write the changed line out to the file
    } else {                             # otherwise
    print OUTPUT &quot;$data[$i]\n&quot;;      # write the unchanged line out to the file

flock(OUTPUT, 8);    # unlock the file
close(OUTPUT);       # and close it

If you find a mistake, have a contribution to make or just want to buy me (or one of the other guys) a beer then drop me a line and I'll incorporate your change or whatever.


Back to Perl FAQ Index
Back to Perl Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close