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
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: kaavannan perl blogspot, perl add new line to string, perl append new line to file, perl insert line in file after match, perl script to insert line into file