The following script will add text to the beginning of files.
Donwload here. perlheader.pdf
This is done in three steps
1) This script copies the contents of a file to local variable by slurping.
2) The necessary text that needs to be added at the beginning of the file is added.
3) Then the original contents of the file are added using the local variable.
#!/usr/bin/perl
# Filename: header.pl
# Author : A.G.Raja
# Website : agraja.wordpress.com
# License : GPL
# pass files as arguments to this script
# this script will add the header text
use strict;
use warnings;
sub addheader{
my($infile,$header)=@_;
my $text = do { local( @ARGV, $/ ) = $infile ; <> } ;
open(FILE,”>$infile”);
print FILE $header;
print FILE $text;
close FILE
}
my $argnum;
foreach $argnum (0 .. $#ARGV){
my $infile = $ARGV[$argnum];
# Edit line below to change header text
my $header = “Header line 1nHeader line 2nHeader line 3n”;
&addheader($infile,$header);
}
# chmod +x header.pl
# ./header.pl *.txt
Donwload here. perlheader.pdf