1830936 Members
1935 Online
110017 Solutions
New Discussion

Re: Playing with files.

 
SOLVED
Go to solution
Raghu Chikkamenahalli
Frequent Advisor

Playing with files.

Hello Experts,
I am trying to insert a few lines, but unable to proceed further.

Here is the situation.
Need to Insert a header into multiple files.

For example, the header which needs to be inserted is stored in a file called "header.txt" and the contents of this header.txt ( 5 lines ) needs to be inserted at the beginig (i.e line no 1 )to the all the files. Tried with sed, some how managed to insert a line using"i", but failed to do it using the header.txt as input to the sed command. Kindly show some pointers to achieve this.

ex.
contents of header.txt
#-----------------------------------
# Executes only in HP-UX
# Author:
# Date:

#-----------------------------------

above mentioned text needs to be inserted in let us say, 10 shell scripts ( this won't be empty ).
4 REPLIES 4
Matti_Kurkela
Honored Contributor

Re: Playing with files.

For that, you don't even need sed.

A small script can do the job:

------------
#!/bin/sh
HEADERFILE="$1"
shift

for FILENAME in "$@"
do
mv "$FILENAME" "$FILENAME.orig" \
&& cat "$HEADERFILE" "$FILENAME.orig" >"$FILENAME" \
&& rm "$FILENAME.orig"
done
------------

Save the script e.g. as "addheader.sh" and use "chmod +x addheader.sh" to mark it executable.

Arguments of this script:
- first argument: the file that contains the headers
- all the other arguments: names of files where the header must be added

The maximum number of arguments is limited by the maximum command line length accepted by the OS. As wildcards are expanded by the shell before executing the command line, you can use wildcards as usual.

Examples of use:

./addheader.sh header.txt file1

./addheader.sh header.txt file1 file2 somedir/file3

./addheader.sh header.txt somedir/*.sh

MK
MK
James R. Ferguson
Acclaimed Contributor

Re: Playing with files.

Hi:

Perl will handle your requirements. You can update your files "in-place" and specify multiple files to be handled in one-pass:

# perl -pi.old -e 'BEGIN{$header="/tmp/header.txt";open(FH,"<",$header) or die "$!\n";@hdr=};print @hdr if $.==1;close ARGV if eof' file1 file2 file3...

If you prefer a more readable layout:

# cat ./filter
#/usr/bin/perl
use strict;
use warnings;
my @hdr;
BEGIN {
my $header = "/tmp/header.txt";
open( FH, "<", $header ) or die "$!";
@hdr = ;
}
print @hdr if $. == 1;
close ARGV if eof
1;

...run as :

# ./filter file1 file2...

That is, pass the file names to be changed. A backup copy will be retained as .old.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor
Solution

Re: Playing with files.

Hi (again):

Ooops, sorry, the "expanded" Perl variation wasn't fully fleshed-out:

# cat ./filter
#!/usr/bin/perl -i.old
use strict;
use warnings;
my @hdr;
BEGIN {
my $fh;
my $header = "/tmp/header.txt";
open( $fh, "<", $header ) or die "$!";
@hdr = <$fh>;
}
while (<>) {
print @hdr if $. == 1;
print;
close ARGV if eof;
}
1;

Regards!

...JRF...
Raghu Chikkamenahalli
Frequent Advisor

Re: Playing with files.

Hi,

Thanks for the pointers. It's really helped a lot to complete my activity.

Many Thanks,
Regards,
Raghu