Perl:How to insert a line to a file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl:How to insert a line to a file.
# 1  
Old 12-14-2010
Perl:How to insert a line to a file.

Hi, Perl is new to me. I am trying to insert a line to a file.
Example: I have a file (trial.txt), content:
Code:
ZZZZ
AAA
DDDD

I am trying to insert CCC below AAA.
MY perl command:
Code:
open (FILE,"+>>C:\\Documents and Settings\\trial.txt\n")|| die "can't open file"; 
while(<FILE>) 
{         
    if(/AAA/) 
   { 
     print FILE "CCC\n";  
    } 
}#end while 
close(FILE);

The output I get:
Code:
ZZZZ
AAA
DDDD
ZZZZ
AAA
CCC

Where else what I need is:
Code:
ZZZZ
AAA
CCC
DDDD

It appends the whole file again and replaces the next line after AAA with CCC, where else what I need is to insert CCC in the existing file below AAA. Thanks to advise. Smilie

Last edited by Scott; 12-14-2010 at 05:45 PM.. Reason: Please use code tags
# 2  
Old 12-14-2010
Im afraid you cannot simply insert lines into a file in this manner - you have to either read the whole file into memory and write back the new file, or you have to write the output to a temporary file.
Here is an example of the former...
Code:
#!/usr/bin/perl
open(FILE,"foo.txt") || die "can't open file for read\n"; 
my @lines=<FILE>;
close(FILE);
open(FILE,">foo.txt")|| die "can't open file for write\n";
foreach $line (@lines) {
    if($line =~ /AAA/) {
        print FILE "CCC\n";
    } else {
        print FILE $line;
    }
}#end foreach
close(FILE);


Last edited by citaylor; 12-14-2010 at 04:45 PM.. Reason: typo
# 3  
Old 12-14-2010
Thanks citaylor Smilie.. tried applying your example, but it still replaces the AAA with CCC, instead of adding the CCC below AAA. Meanwhile, I am trying to google for any tips on this, no luck so far..
# 4  
Old 12-14-2010
Sorry, I misunderstood...

Code:
#!/usr/bin/perl
open(FILE,"foo.txt") || die "can't open file for read\n"; 
my @lines=<FILE>;
close(FILE);
open(FILE,">foo.txt")|| die "can't open file for write\n";
foreach $line (@lines) {
    print FILE "CCC\n" if($line =~ /AAA/);
    print FILE $line;
}#end foreach
close(FILE);

# 5  
Old 12-14-2010
If you want AAA after CCC, then these two lines need to be swapped
Code:
    print FILE "CCC\n" if($line =~ /AAA/);
    print FILE $line;

# 6  
Old 12-15-2010
Here ..

Code:
#!/usr/bin/perl -w
open(IFILE,"<test.abc") || die "can't open file for read\n"; 
open(OFILE,">abc.out") || die "can't open file for writing\n"; 
while (<IFILE>){
     if(/AAA/) {
        print OFILE $_;
        print OFILE "CCC\n";
    }else {
        print OFILE $_;
}
}
close(IFILE);
close(OFILE);


Code:
$ cat test.abc 
ZZZZ
AAA
DDDD
$ cat abc.out 
ZZZZ
AAA
CCC
DDDD

# 7  
Old 12-16-2010
MySQL Thanks all :)

Got it now... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

2. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

3. Shell Programming and Scripting

Insert a new line before every 5th line in a file

Hi, I need to insert a new line containing the string "QUERY" above every 5 lines. The below piece of code inserts a new line after every 5th line awk '{print $0} !(NR%5) {print "QUERY"}' sed 'n;n;n;n;G;' --> I do not know how to give "QUERY" string here But I need to insert it before... (4 Replies)
Discussion started by: royalibrahim
4 Replies

4. Linux

How to insert new line in perl

HI, I have a text file in which I have removed all new lines as I would like to introduce a new line at the end of each record in the file. There is no common end line for all the records. A new record will start by *RECORD*. So I want to introduce a new line before this line *RECORD*. So Can... (2 Replies)
Discussion started by: kaav06
2 Replies

5. Programming

insert line into a file

how to insert a line of text that is next to the current line(file pointer pointing to) in the file ?? :wall: ex: suppose a file named 'Sample' has the following content in it. this is to give clear idea about the problem if file pointer is pointing to the first line then i want to... (3 Replies)
Discussion started by: kavitha rao
3 Replies

6. Shell Programming and Scripting

to insert some word somewhere in the line with shell (or perl)

hello mighty all there is a line of 50 words and i need to take a random number of words from the beginning (20 words for example) then put my word then add other 10 words from the continue then add another my special word then add 20 words till the end.. my own knowledge base can say it is... (12 Replies)
Discussion started by: tip78
12 Replies

7. Shell Programming and Scripting

Insert line into file

Hi, My File 'temp.txt' contents are like this. <Managers> Mng={{FIL|FAVEI.mng|111}|15.000000|17.000000|17.000000| Mng={{FIL|FAPSV.mng|222}|3.000000|0.000000|0.000000|0.000000| Mng={{FIL|FAVIF.mng|333}|8.000000|8.000000|8.000000|8.000000|... (3 Replies)
Discussion started by: vinay123
3 Replies

8. Shell Programming and Scripting

how to insert text before first line in perl

Hello all im doing simple parsing on text file , but now I need to insert string before the first line of the text file , how can I do that in perl? (3 Replies)
Discussion started by: umen
3 Replies

9. Shell Programming and Scripting

insert a line in a file

Hello guys, Need to know how to insert a line at top of the file, without using temp files. Can we do it on the fly? Regards, Rishi (7 Replies)
Discussion started by: RishiPahuja
7 Replies

10. UNIX for Advanced & Expert Users

Insert a line as the first line into a very huge file

Hello, I need to insert a line (like a header) as the first line of a very huge file (about 3 ml rows). I am able to do it with sed, but redirecting the output and creating a new file takes quite some time. I was wondering if there was a more efficient way of doing it? Any help would be... (3 Replies)
Discussion started by: shriek
3 Replies
Login or Register to Ask a Question