- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Playing with files.
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2008 05:45 AM
10-07-2008 05:45 AM
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 ).
Solved! Go to Solution.
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2008 06:10 AM
10-07-2008 06:10 AM
Re: Playing with files.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2008 06:11 AM
10-07-2008 06:11 AM
Re: Playing with files.
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=
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
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2008 05:37 AM
10-09-2008 05:37 AM
SolutionOoops, 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2008 06:43 PM
10-09-2008 06:43 PM
Re: Playing with files.
Thanks for the pointers. It's really helped a lot to complete my activity.
Many Thanks,
Regards,
Raghu