File name suffix with a running number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File name suffix with a running number
# 1  
Old 12-27-2009
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
# 2  
Old 12-27-2009
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
# 3  
Old 12-27-2009
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.
# 4  
Old 12-28-2009
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
# 5  
Old 12-28-2009
Quote:
Originally Posted by krishna.saran
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
You can try something like this:

Code:
#!/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}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Prefix/Suffix on same file

Hi, I want to add prefix and suffix on line# 205 using SED or AWK and want to change on the same file without creating new file. This command will be used in the bash script Am using Bash shell Regards Nayaj (3 Replies)
Discussion started by: Nayaj
3 Replies

2. Shell Programming and Scripting

Lftp sftp get - script renames the local file with suffix tilde

Hi, Below script used for sftp get, #/bin/bash USER=xxx PASS=xxx HOST=xxx REMOTE_FILE=$1 LOCAL_FILE_LOC=$2 cd $LOCAL_FILE_LOC lftp sftp://$USER:$PASS@$HOST:10022 -e "get $REMOTE_FILE; bye" If file does not exist in sftp server, and file (same as remote file name) exists in local dir,... (4 Replies)
Discussion started by: vhegde1011
4 Replies

3. UNIX for Dummies Questions & Answers

How do I find the number of processes running on root?

Is there a certain man command I'm missing here? I searched in ps but I couldn't find something that would give me the number of processes running on root. I only want to see the number of processes, not the processes itself. (2 Replies)
Discussion started by: l3monz
2 Replies

4. Shell Programming and Scripting

Finding which is Running number

Hi All, I have a list of data in the listing file and I run row by row to process the record, but how to identify what is the current process line number ? Thanks. (2 Replies)
Discussion started by: ckwan
2 Replies

5. Shell Programming and Scripting

Adding new lines to a file + adding suffix to a pattern

I need some help with adding lines to file and substitute a pattern. Ok I have a file: #cat names.txt name: John Doe stationed: 1 name: Michael Sweets stationed: 41 . . . And would like to change it to: name: John Doe employed permanently stationed: 1-office (7 Replies)
Discussion started by: hemo21
7 Replies

6. Shell Programming and Scripting

How to Control Number of Processes Running

Hi Is there a way to count how many processes a script has started, count how many of these have finished, and make the script wait if their difference goes over a given threshold? I am using a script to repeatedly execute a code (~100x) which converts 2 data files into one .plt which is in... (4 Replies)
Discussion started by: drbones
4 Replies

7. Shell Programming and Scripting

Keep a certain number of background processes running

I've got a bit of code I'm trying to work on... What i want to happen is ... at all times have four parallel mysql dump and imports running. I found the follow code snippet on the forum and modified it to work by starting four concurrent processes but it waits until all four are done before... (7 Replies)
Discussion started by: dgob123
7 Replies

8. Solaris

Number of threads running

Is there any command to find 1) the number of threads running 2) kernel boot mode in solaris box (2 Replies)
Discussion started by: vickylife
2 Replies

9. Shell Programming and Scripting

Determine Number of Processes Running

I am pretty new to unix, and I have a project to do. Part of the project asks me to determine the number of processes running and assign it to a variable. I know how to every part of the project but determine the number of processes running. How can I get just the number of processes... (4 Replies)
Discussion started by: wayne1411
4 Replies

10. Shell Programming and Scripting

How to limit the number of running instances of a script?

I would like to allow only one instance of a script to run at any moment. I've tried the following solution to count the instances but the result is always the number of running instances plus one and I can't find the problem ps -ef | grep $0 | sed '/^$/ d' | sed '/grep/ d' | wc -l Please... (2 Replies)
Discussion started by: oti
2 Replies
Login or Register to Ask a Question