File name suffix with a running number

Hi all,

i have a shell script invoking a pl/sql proc which then, generates a file in a format
DATA-26-12-2009.txt. The shell script is configured as a cron job to run once a day.

If i need to run the script more than once, i need to suffix the generated file with a two digit running number like DATA-26-12-2009-01.txt.

I need to know how to keep a two digit number and increment it, like 01, 02, 03,...09,10 ..etc.

Pls suggest a simple way to do this.

Thanks

hi,

i don't know if this will work for you but had an idea why not store the value of number of files in the dir (ls *.*| wc -l), and append this value to your file name. as a result you will automatically increment the value.

cheers

Thanks for your suggestion. Since the file generated is with current date format dd-mm-yyyy, i need to append with some number before copying to specific directory, in order to avoid overwriting.

I thought of something like this below. Pls suggest whether it is ok.

  1. Check for existence of file with filename format DD-MM-YYYY. If does not exist, create the file and put 00. Increment it and append to file name.
  2. Each time file is generated, take the number in that file(first line) and increment it. Any idea how to do about this ..
  3. Append the incremented number to the file name and move the file to a specific loc.

Hi,

just had a query, suppose the number of files with the current date is large. then in that case you will have to obtain the maximum number by traversing the file names and sorting them to find the maximum number. wouldn't that be an overhead ?

had another idea:-
1) create a dir with the current date in a temp loc.
2) everytime you create a file, add a new file entry to this dir (any name will do). As a result when you create a new file , you only have to see the number of files in this dir, and then increment it by 1.
3) after midnight, delete the dir and create a new one by the new date. (cronjob)
this will hopefully save you some time.

cheers

You can try something like this:

#!/bin/ksh

file="DATA-"$(date "+%d-%m-%Y-")
n=1
ext=".txt"
ls ${file}* | while read i
do
  fn=$(printf "%02d" n)
  if [ ${file}${n}${ext} = ${i} ]
  then
    n=$(( $n + 1 ))
  fi
done

fn=$(printf "%02d" n)
echo "New file:" ${file}${n}${ext}