Operating System - HP-UX
1834018 Members
2081 Online
110063 Solutions
New Discussion

appending in the middle of the file

 
andi_1
Frequent Advisor

appending in the middle of the file

Hi guys,

Before I send email in my script I need to remove first 3 lines and I also need to add one paragraph after words Dear Name, e.g.

Current format:
yara
yara
yars
Dear Name,
hello1
hello2

Needed Format:
Dear Name
hello0 // new paragraph is added
hello1
hello2

I know how to remove first three lines, but how can I append a paragraph?

Here is my code in Perl:
$rc = 0xffff & system("tail +3 $FILENAME | mailx -s \"$mailSubject\" $adminMail ");

Thanks a lot!
5 REPLIES 5
Ceesjan van Hattum
Esteemed Contributor

Re: appending in the middle of the file

delete first 3 lines, insert paragraph:

cat $PARAGRAPH > $NEWLETTER
awk '{if (NR>3) print $0}' $OLDLETTER >> $NEWLETTER

Regards,
Ceesjan
Rodney Hills
Honored Contributor

Re: appending in the middle of the file

Here is a small program that eliminates using "tail" command.

open(INP,"<$FILENAME") or die "no open $FILENAME";
open(OUT,"|mailx -s \"$mailSubject\") or die "no open mailx";
while() {
next if $. <= 3;
print OUT $_;
print OUT "hello0 // new paragraph is added\n" if /^Dear Name/;
}
close(INP); close(OUT);

Hope this helps...

-- Rod Hills
There be dragons...
Dave La Mar
Honored Contributor

Re: appending in the middle of the file

andi -

I used this crude method to insert, at a given line in a file, a string.

string="* FAILURE IN $SCRIPT_NAME ON $HOST CALL $PROGRAMMER"
ed << EOF
e /path_to/file_name
4i
$string
.
w /path_to/file_name
q

I suppose $string could be your paragraph. In this example it is inserted at line 4 of /path_to/file_name.

Hope this helps.

Best of Luck.

dl

"I'm not dumb. I just have a command of thoroughly useless information."
Don Dwyer
New Member

Re: appending in the middle of the file

andi,

Call this script:

#!/bin/ksh

in_file=$1 # File to be split
parse_string="$2" # String to Parse
para_file=$3 # File with new paragraph
new_file=$4 # Output file
split_file=`mktemp` # Holds the split data in two files

csplit -s -f ${split_file} ${in_file} /"${parse_string}"/+1 # Split the file

cat ${split_file}00 > ${new_file}
cat ${para_file} >> ${new_file}
cat ${split_file}01 >> ${new_file}

James Beamish-White
Trusted Contributor

Re: appending in the middle of the file

Here's a way to create a file with your requirements, given your yara... file as file1:

sed -n 4p file1 > sendfile
echo hello0 >> sendfile
sed -n 5,6p >> sendfile

You'll have to change the 6 to however long your file is, or otherwise get it dynamically by using:

fileLength=`nl -ba file1 | tail -1 | awk '{print $1}'`

and then change the last bit to be
sed -n 5,"$fileLength"p >> sendfile


Cheers!
James
GARDENOFEDEN> create light