agraja.wordpress.com

Perl script to add Header to files

In Bookmarks on March 5, 2008 at 12:23 pm

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

  1. Hello webmaster
    I would like to share with you a link to your site
    write me here preonrelt@mail.ru

  2. The script is quite useful, although it has got a minor bug into it.
    The line below
    my $text = do { local( @ARGV, $/ ) = $infile ; } ;
    should be changed to
    my @text = do { local( @ARGV, $/ ) = $infile ; } ;

    Please use @text instead of $text

  3. Any port in a storm
    clenbuterol [url=http://pharmacy-and-pharmacy.com/clenbuterol/]clenbuterol[/url] tussionex [url=http://pharmacy-and-pharmacy.com/tussionex/]tussionex[/url] protopic [url=http://pharmacy-and-pharmacy.com/protopic/]protopic[/url] ciprobay [url=http://pharmacy-and-pharmacy.com/ciprobay/]ciprobay[/url] temazepam [url=http://pharmacy-and-pharmacy.com/temazepam/]temazepam[/url] targretin [url=http://pharmacy-and-pharmacy.com/targretin/]targretin[/url] hoodia [url=http://pharmacy-and-pharmacy.com/hoodia/]hoodia[/url]