Operating System - HP-UX
1828670 Members
2397 Online
109984 Solutions
New Discussion

Script to append data to the first line in a file

 
Usha_madhavan
New Member

Script to append data to the first line in a file

Hi all,

We need to generate a script which will append to first line in a file in the 100th position.

We have an ASCII file generated from the ERP system. The ASCII file can have n number of lines in it. We need to append some contents to the first line in the file before processing the file further.

Every flat file will have an identifier in the first line. For example: Purchase order will have “Purchase Order” as the first line. BOL will have “Bill of Lading” as identifier in the first line. We need to append the string “PRINTER: ” in the first line starting from 100th position.

Request your help in writing the script.

Regards,
Usha
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: Script to append data to the first line in a file

Hi Usha:

I believe that this is answered here:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1088911

Please see my last post therein.

Regards!

...JRF...
Usha_madhavan
New Member

Re: Script to append data to the first line in a file

Hi James,

Thanks a lot for the quick response.

But when I execute the script I get an error as follows:

Syntax error at line 3 : `{' is not expected.

What could be the possible error?

I am a novice in UNIX.

Regards,
Usha
Usha_madhavan
New Member

Re: Script to append data to the first line in a file

Hi James,

I am sorry the error is on line 2.

Syntax error at line 2 : `{' is not expected.

Regards,
Usha
spex
Honored Contributor

Re: Script to append data to the first line in a file

Usha,

#!/usr/bin/sh
FILE=
TEXT=
FILET=/tmp/$(basename ${FILE}).tmp
echo "$(echo $(head -1 ${FILE}) $(perl -e 'print " "x100') | cut -c1-100)" ${TEXT} > ${FILET}
sed -n '2,$p' < ${FILE} >> ${FILET}
cp -f ${FILET} ${FILE}
exit 0

PCS
James R. Ferguson
Acclaimed Contributor

Re: Script to append data to the first line in a file

Hi Usha:

If you cut-and-paste exactly this (from the post I referenced) into an empty file:

#!/usr/bin/perl -ni.old
if ( $. == 1 ) {
if ( length($_) < 100 ) {
chomp;
printf "%-100s%s\n", $_, "WORD";
}
else {
s/(.{100})(.*)/$1WORD$2/;
print;
}
next;
}
print;

...then having called the script "myfilter", you should do:

# ./myfilter myfile

...where "myfile" is the file to be changed.

If you added "#!/usr/bin/sh" at the top of the script, then that is your problem! Copy and paste exactly what I wrote.

Regards!

...JRF...