Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UNIX Scripting

Status
Not open for further replies.

kevinlong

Programmer
Mar 23, 2007
6
GB
I have a database system that generates a lot of reports on a daily basis in the format of ct6400a.13544

I want to create a script that reads the names of the files and converts them to something more meaningful, but still keeps the original information as part of the new filename.

ct6400.13544

would become: -

ct6400 (13544) - Cost Analysis.wri

Any clues? I'm faily new to UNIX scripting, so any help would be greatly appreciated. Thanks.

 
And what have you tried so far and where in your code are you stuck ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
One initial recommendation - don't use spaces in unix file names.

I want to be good, is that not enough?
 
Where does the "Cost Analysis.wri" automagically come from?

ct? First line of the contents of the file?

Some more input please...

Als better not use brackets and spaces in filenames, you'll get into all sorts of problems...


HTH,

p5wizard
 
Assuming that you have a finite list of files in the format: ct6400a.13544 and know what they really are IE Cost Analysis, you could do something like this:

Code:
DSTAMP=`date "+%H%M_%d%m%y"`
for FILE in `ls`
do
  case $FILE in
     ct6400.13544) mv $FILE "ct6400 (13544) - Cost Analysis.wri" ;;
        some.file) mv $FILE "some file.name" ;;
       other.file) mv $FILE "other file.name" ;;
                *) echo "No rename description for: $FILE";;
  esac
done

if you think you might want/need multiple copies of ct6400 (13544) - Cost Analysis.wri, then your the modify the above move commands to look something like this:

Code:
mv $FILE "${DSTAMP}_ct6400 (13544) - Cost Analysis.wri" ;;

This will give you a file with the time and date as part of the file name. Unless multiple files are generated withing the same minute, you could have as many DSTAMP_... files as disk space permits.

Be sure to add any pre/post rename commands as needed.
 
Ummm... that last line in my previous post should read:

Be sure to add any pre/post commands as needed.
 
arg.... one more thought...

Even though you techically "can" use spaces, parens, etc. in a file name, like p5wizard said, if you want to avoid future problems, leave the spaced, etc. out.
 
Thanks to SBREWS that looks like the kind of thing I'm after however the 13544 part of the filename is going change all the time. When the system saves the file as ct6400.13544 on a Monday, when it re-runs on a Tuesday the file will be ct6400.13699 for example as the number is a unique identifier for that perticular report.

Is there any way I can utilise your script and still preserve the number part of the filename? If I use a * wildcard for the first part of each line, i.e.

ct6400.*) mv $FILE ...

How will I get it to pick that number up again when I rename the file?

I'm running Sun Solaris 9.
 
Kevin,

The script below separates the prefix and suffix parts of your original filename; you could combine it with sbrew's case statement to decide what meaningful name (shown as 'Cost_Analysis' below) you want for each prefix (assuming that the prefix indicates what report created it).
Code:
for f in *
do
  PREFIX=`echo $f | cut -d. -f1`
  SUFFIX=`echo $f | cut -d. -f2`
  cp $f ${PREFIX}_${SUFFIX}_Cost_Analysis.wri
done
HTH
Simon
 
Thanks very much for all your help everyone.

REPHOME=/export/home/aisdba/tmp
REPTEMP="$REPORT.$JOB"
cat > $REPHOME/$REPTEMP
DSTAMP=`date "+%H%M_%d%m%y"`
for FILE in `ls /export/home/aisdba/tmp`
do
case $FILE in
cr6010*.$JOB) mv $REPHOME/$FILE "$REPHOME/$REPTEMP-Payment_Production_$DSTAMP.wri" ;;
...
*.$JOB) echo "No rename description for: $FILE" ;;
esac
done

Got it working as above. $REPORT and $JOB are variables I found that I can pick up from the DB when it saves the file so I've utilised those. Combined as you can see with SBREWS script code (thanx 4 that mate) the whole thing works a treat.

Kev
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top