Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-28-2008, 07:34 AM   #1
LQ Newbie
Registered: Jan 2008
Posts: 27

Rep: Reputation: 15
Question while loop creates a problem for a file opened in append mode for writing in PERL


This is my code.

open (TEMP_WRITE,">>record");
while ( <TEMP_WRITE> ) {
print $_;
if ($_ =~ /India/) {
print TEMP_WRITE "\n"."british\t\tHistory"."\n";


I'm trying to match a string "India" in the file record and once matched appending a new line with a data "shiju history".

This is only an example. My intention is to match a string and when found add a new data on the next line to the matched string.
In the above example, if the file "record" had ,

India History
China Wonders
Australia Cricket

On matching "India", a new line should be appended as

India History
British history.
and the rest...

Problem: The while loop to read the lines of the file works only for "reading" and not for writing/appending. The lines are not read at all.The output is blank for the above code.

Opening read mode with 1 more file handler and directing the output to another file is one solution. But is there no way , i can write into a file directly after reading????

Ur replies are highly appreciated.

~ gaynut
Old 07-28-2008, 12:39 PM   #2
Registered: Sep 2007
Location: Berlin
Distribution: Slackware
Posts: 509

Rep: Reputation: 41
If a file is opened to append to it, the file descriptor sits on the end of the file waiting for your data to be appended. (At least I think this is the case, I may be wrong.)

You'll have to seek (perldoc -f seek) back to the beginning of the file - but you really don't want to do this.

I strongly recommend not writing on the same data you're reading from - open the file for reading, put it into some convenient data structure - wether array or a scalar depends on what your're going to do - change it in there and write afterwards the entire text.

open($fh_read, "<yourfile");

make an array to add stuff linewise:

@all_lines = <$fh_read>;

or put all in one scalar (sometimes _very_ useful, especially with reg ex flags like s///sm <--):

    local $/; #enable slurp mode - see perldoc perlvar for details
    $all_lines = <$fh_read>;
Now use your loop, change the data and _afterwards_ write it to the file - override brutally it if you're confident or copy first and write a new one.

Last edited by Su-Shee; 07-28-2008 at 12:40 PM.
Old 07-28-2008, 07:26 PM   #3
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,396

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
A (flat text) file is what you see in an editor. You can't insert a line in the middle.
You have to open for read ("<"), read line by line, write out each line to a new file and (in the write loop) add any extra lines you want to add.

If you have a small enough file (ie fits easily into memory) you can read the whole file into an array and work your way through that.
In that case, you could close the file for input and re-open for output, thereby overwriting it. Be sure you have a backup first whilst testing(!).

Example read into an array:
    open( REQ_FILE, "<$infile_name" ) or
                exit_with_error("Unable to open file: $infile_name: $!\n");
    @file_recs = <REQ_FILE>;
    chomp(@file_recs);      # remove newline chars
    close(REQ_FILE) or
                exit_with_error("Unable to close file: $infile_name: $!\n");


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Perl] append to new file noir911 Programming 5 02-08-2007 06:38 AM
[Perl] append columns to file noir911 Programming 3 02-08-2007 05:29 AM
Writing to a file in Kernel Mode Caliber Programming 2 04-19-2005 09:08 AM
problem with writing into a file using socket(perl) akaash Programming 3 04-08-2004 06:06 AM
cgi perl : I cant get perl to append my html file... the_y_man Programming 3 03-22-2004 05:07 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:40 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration