Sunday, July 17, 2011

Read and write in the same file perl

Content of hello.dat file  before running the command:

hello how are you
wonderful beautiful
great
award great
awesome

Content of the perl file:

open(FILE,"+<hello.dat");
# "+<"  option has to be used for read and writing to the file
@lines=<FILE>;  
foreach(@lines)
{
chop;
print;   # it is same as print $_;
print "\n";
}
print FILE "\n"."karthik";
close(FILE);



Output Execution:
G:\perl_programs>perl perl_file.pl
hello how are you
wonderful beautiful
great
award great
awesom

G:\perl_programs>

Content of the hello.dat file after execution:

hello how are you
wonderful beautiful
great
award great
awesome
karthik

1 comment:

  1. how to write "karthik" in between the lines of "award great"and "awesome".

    ReplyDelete