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?
That shouldn't be difficult... you need a temporary file. You read the file1.txt you write your string in file2.txt and after that you copy the content of file1.txt into file2.txt.
I think that Perl has some options for reading an entire text file into a variabile, but I'm not sure... if there is suchs function you won't need file2.txt.
This will open the file, and parse it line by line. You mean something like this? ( Replace $0 with the name of the file you want to parse - filename must be in quotes. )
$ cat x.pl
#!/usr/bin/env perl
my $infile;
$infile = $0;
open( IN , "< $infile") or die "Can't open $infile. $!";
my $line;
my $i = 0;
while( $line = <IN> ) {
$i++;
chomp $line;
printf STDOUT "string$i $line\n";
}
close(IN);
The result:
$ x.pl
string1 #!/usr/bin/env perl
string2
string3 my $infile;
string4
string5 $infile = $0;
string6
string7 open( IN , "< $infile") or die "Can't open $infile. $!";
string8
string9 my $line;
string10 my $i = 0;
string11 while( $line = <IN> ) {
string12 $i++;
string13 chomp $line;
string14
string15 printf STDOUT "string$i $line\n";
string16 }
string17 close(IN);
string18
string19
Dear Umen,
You can simply use a flag value and put the line you want to write only for the first time.
regards
Apoorva Kumar