Operating System - HP-UX
1831148 Members
2630 Online
110020 Solutions
New Discussion

Re: Korn Shell Scripting problem

 
SOLVED
Go to solution
Jeremy_5
New Member

Korn Shell Scripting problem

Hello All-

I need a line to add to the following shell script that will rename files from this format

JJONES.12345
THONEYCUTT.44332
RCRASUS.55555

to this format

12345.pdf
44332.pdf
55555.pdf

respectively.

Basically dropping the original alphabetic prefix, making the numeric extension the new prefix, and adding a .pdf extension. This is not a conversion job, just a renaming.

The original filenames are passed to the script in variable format by an oracle application. The file needs to be renamed before it is copied to it's destination (invoice_in)

Any help on this issue would be greatly appreciated!

--BEGIN KORN SHELL SCRIPT--

#!/usr/bin/ksh
# A simple script to copy a file submited to invoicetopdf to the invoice_in
# directory. It takes the first positional parameter as its only argument.

echo "I am in the script" >> /tmp/inv.err
echo $# >> /tmp/inv.err
echo $* >> /tmp/inv.err
echo "File name is: $1" >> /tmp/inv.err
cp $1 $APPL_TOP/ecan/invoice_in/in/ >> /tmp/inv.err
print $? >> /tmp/inv.err

--END OF SCRIPT--


Thanks-Jeremy

ifjq216@hotmail.com
jeremy@neosurf.net
6 REPLIES 6
John Palmer
Honored Contributor
Solution

Re: Korn Shell Scripting problem

Your processing loop should be something like:-

for FILE in ${*}
do
SUFFIX=${FILE#*.} # leave chars after final .
cp (or mv) ${FILE} /${SUFFIX}.pdf
done

Regards,
John
Rodney Hills
Honored Contributor

Re: Korn Shell Scripting problem

for file in * ; do
newnam="${file##*.}.pdf"
mv $file $newname
done
There be dragons...
Patrick Wallek
Honored Contributor

Re: Korn Shell Scripting problem

You could also use the cut feature:

var1="`echo $1 | cut -d. -f2`.pdf"
mv $1 $var1
Curtis Larson
Trusted Contributor

Re: Korn Shell Scripting problem

filename=$(cat $yourfilename | sed -n 's|^[A-Z]*\.\([0-9]*\)$|\1.pdf|p)
if [[ -z $filename ]] ;then
invalid file name message
exit
fi
James A. Donovan
Honored Contributor

Re: Korn Shell Scripting problem

Unless you know for sure that each numeric suffix of the old filename will be unique from any other suffix, I would also add code to check for the existance of the new filename before you write it. Otherwise you may end up overwriting other output.
Remember, wherever you go, there you are...
Joseph C. Denman
Honored Contributor

Re: Korn Shell Scripting problem

a million ways to skin a cat.

newfile=`echo $file | awk -F \. '{print $2}'`.pdf
mv $file $newfile

...jcd...
If I had only read the instructions first??