I am opening the file in append mode. I need to replace lines 2,3, and 4 in the file, and later I need to add the new data at the end of the file.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I think this is the FAQ answer that I’ve reposted to Stack Overflow the most. The perlfaq5 has the answer to How do I change, delete, or insert a line in a file, or append to the beginning of a file?.
Forget about the append mode stuff. That’s just going to make your life harder.
The basic idea of inserting, changing, or deleting a line from a text file involves reading and printing the file to the point you want to make the change, making the change, then reading and printing the rest of the file. Perl doesn’t provide random access to lines (especially since the record input separator,
$/, is mutable), although modules such as Tie::File can fake it.A Perl program to do these tasks takes the basic form of opening a file, printing its lines, then closing the file:
Within that basic form, add the parts that you need to insert, change, or delete lines.
To prepend lines to the beginning, print those lines before you enter the loop that prints the existing lines.
To change existing lines, insert the code to modify the lines inside the while loop. In this case, the code finds all lowercased versions of "perl" and uppercases them. The happens for every line, so be sure that you’re supposed to do that on every line!
To change only a particular line, the input line number,
$., is useful. First read and print the lines up to the one you want to change. Next, read the single line you want to change, change it, and print it. After that, read the rest of the lines and print those:To skip lines, use the looping controls. The next in this example skips comment lines, and the last stops all processing once it encounters either
__END__or__DATA__.Do the same sort of thing to delete a particular line by using next to skip the lines you don’t want to show up in the output. This example skips every fifth line:
If, for some odd reason, you really want to see the whole file at once rather than processing line-by-line, you can slurp it in (as long as you can fit the whole thing in memory!):
Modules such as File::Slurp and Tie::File can help with that too. If you can, however, avoid reading the entire file at once. Perl won’t give that memory back to the operating system until the process finishes.
You can also use Perl one-liners to modify a file in-place. The following changes all ‘Fred’ to ‘Barney’ in inFile.txt, overwriting the file with the new contents. With the
-pswitch, Perl wraps a while loop around the code you specify with-e, and-iturns on in-place editing. The current line is in$_. With-p, Perl automatically prints the value of$_at the end of the loop. See perlrun for more details.To make a backup of inFile.txt, give
-ia file extension to add:To change only the fifth line, you can add a test checking
$., the input line number, then only perform the operation when the test passes:To add lines before a certain line, you can add a line (or lines!) before Perl prints
$_:You can even add a line to the beginning of a file, since the current line prints at the end of the loop:
To insert a line after one already in the file, use the
-nswitch. It’s just like-pexcept that it doesn’t print$_at the end of the loop, so you have to do that yourself. In this case, print$_first, then print the line that you want to add.To delete lines, only print the ones that you want.