Operating System - HP-UX
1829362 Members
3150 Online
109991 Solutions
New Discussion

script to sequence log files

 
Vin_5
Occasional Advisor

script to sequence log files

Hi,

I'm trying to simplify a script I have for sequencing files, maybe someone has one already or advice on the best way to go about it?

The script applies sequence numbers to log files each day from 001->100.

File names are of the format .log, so I want to change them to 001.log up to 100.log, and then start again at 001 by running the script each day.

Any help much appreciated,

Vin.
4 REPLIES 4
Massimo Bianchi
Honored Contributor

Re: script to sequence log files


Try something like this (no hpux on hands)


COUNT=1

while $COUNT < 101
do
DATE=$( date +%a%I )
touch /dir/$COUNT$DATE.log
let COUNT=COUNT+1
done



HTH,
Massimo
James R. Ferguson
Acclaimed Contributor

Re: script to sequence log files

Hi:

Assuming that your files were named like '20030612_hhmmss' something like this would do:

#!/usr/bin/sh
typeset -i N
typeset D=`date +%Y%m%d`
cd /tmp
for F in `ls f${D}*.log`
do
(( N=N+1 ))
mv ${F} ${N}${F}
done
exit 0

Regards!

...JRF...
Jean-Louis Phelix
Honored Contributor

Re: script to sequence log files

Hi,

first of all I would create 100 empty files (001.log to 100.log). then it would be easy to run the script every day :

#!/usr/bin/sh
I=1
while [ $I -le 100 ]
do
touch $(printf "%3.3d.log" $I)
I=$((I+1))
sleep 1
done

without sleep, it seems that files could have creation time in the wrong order. It's run only once, so ...

Then, you can run this script every day. Argument should be the new log file, or you can directly get it from the script using a ls pattern or a date :

#!/usr/bin/sh
TARGET=$(ls -rt [01][0-9]*.log | line)
mv $1 $(echo $TARGET | cut -c1-3)_$1
rm $TARGET

Regards.
It works for me (© Bill McNAMARA ...)
Martin Robinson
Frequent Advisor

Re: script to sequence log files

A useful hint is the typeset command in ksh. Use 'typeset -Z3 name' to make sure the variable 'name' is always printed with 3 digits, leading zeros.

ie:
typeset -Z3 name
a=1
echo $a
001

This will simplify the filenaming side of things.