Saturday, 2 November 2019

perl insert new line at beginning of existing data file

Method 1:

1.perl -pi -e 'print "perl one liner is added" if $. == 1'  file.txt

Method 2:

open my $file1,  '<',  $file1.txt;
open my $file2, '>', "$file2.txt";

print $file2 "perl one liner is added";

while( <$file1> ) {
    print $file2 $_;
}
close $file2;
close $file1;

Method 3:

echo "perl one liner is added" | perl -0 -i -pe 'BEGIN {$input = <STDIN>}; print $input' file.txt

Method 4:

open(M,"<","file1.txt");
@m = <M>;
close(M);
open(M,">","file2.txt");
unshift (@m,"Perl Coder is always magic\n");
print M @m;
close(M);


kaavannan perl blogspot

Labels: , , , ,

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home