Renaming a file with sequence number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming a file with sequence number
# 1  
Old 09-05-2015
Renaming a file with sequence number

Hi team,

I need a script for renaming a file with sequence number. script get a file from one directory, /home/billing/Cmm/sms/sms_tmp, append sequence no at the end of file name and move a file to other directory, /home/billing/Cmm/sms/.

Actual file is cdr201508271527, and file after renaming cdr201508271527_00001. Script also have capability to save the state of sequence number so when next time it will increment it will start increment from the last file transferred. I have a multiple files in /home/billing/Cmm/sms/sms_tmp. Each file can be identify with time stamp.

Last edited by rbatte1; 09-11-2015 at 09:49 AM.. Reason: ICODE tags, spelling etc.
# 2  
Old 09-05-2015
Hello mfaizen40,

Following may help you in same, but I didn't test it as no system as of now with me.
Code:
ACTUAL_PATH=/home/billing/Cmm/sms
TMP_PATH=/home/billing/Cmm/sms/sms_tmp
SEQ_FILE=/home/billing/Cmm/sms/squence_file.txt
LIST_FILES=/home/billing/Cmm/sms/files_list
PREVIOUS_LIST=/home/billing/Cmm/sms/previous_file_list
FILES_NEEDS_TO_BE_CREATED=/home/billing/Cmm/sms/files_needs_to_be_created
find $TMP_PATH -type f -printf "%f\n" 2>/dev/null > $LIST_FILES
VAL=`cat $SEQ_FILE`
if [[ -s $PREVIOUS_LIST ]]
then
	awk 'FNR==NR{A[$0];next} !($0 in A){print $0}' $PREVIOUS_LIST $LIST_FILES > $FILES_NEEDS_TO_BE_CREATED
	while read line
	do
		let "VAL = VAL + 1"	
                VAL1=`printf "%05d\n" $VAL`	
                mv $TMP_PATH"/"$line $ACTUAL_PATH"/"$line"_"$VAL1
	done < "$FILES_NEEDS_TO_BE_CREATED"
else
	while read line
	do
		let "VAL = VAL + 1"
                VAL1=`printf "%05d\n" $VAL`	
                mv $TMP_PATH"/"$line $ACTUAL_PATH"/"$line"_"$VAL1
	done < "$PREVIOUS_LIST"
fi	
        

printf "%05d" $VAL1 > $SEQ_FILE
mv $LIST_FILES $PREVIOUS_LIST

You can save above code as a script and give that script as permission according to
your convince and try it out, hope this is helpful. Please do let us know how it goes for you.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-05-2015
Sounds like: Appending sequence number at the end of file name

Any attempts from your side?
# 4  
Old 09-05-2015
Hello mfaizen40,

Just adding same above code with a use of function to reuse of code.
Hope this will be helpful.
Code:
ACTUAL_PATH=/home/billing/Cmm/sms
TMP_PATH=/home/billing/Cmm/sms/sms_tmp
SEQ_FILE=/home/billing/Cmm/sms/squence_file.txt
LIST_FILES=/home/billing/Cmm/sms/files_list
PREVIOUS_LIST=/home/billing/Cmm/sms/previous_file_list
FILES_NEEDS_TO_BE_CREATED=/home/billing/Cmm/sms/files_needs_to_be_created
find $TMP_PATH -type f -printf "%f\n" 2>/dev/null > $LIST_FILES
VAL=`cat $SEQ_FILE`
VAL1=`printf "%05d\n" $VAL`	

MAKE_CHECK_FILE() {
Input_file=$1
	while read line
	do
		let "VAL = VAL + 1"	
                VAL1=`printf "%05d\n" $VAL`	
                mv $TMP_PATH"/"$line $ACTUAL_PATH"/"$line"_"$VAL1
	done < "$Input_file"

	           }


if [[ -s $PREVIOUS_LIST ]]
then
	awk 'FNR==NR{A[$0];next} !($0 in A){print $0}' $PREVIOUS_LIST $LIST_FILES > $FILES_NEEDS_TO_BE_CREATED
        MAKE_CHECK_FILE $FILES_NEEDS_TO_BE_CREATED
else
        MAKE_CHECK_FILE $PREVIOUS_LIST
fi	
        

printf "%05d" $VAL1 > $SEQ_FILE
mv $LIST_FILES $PREVIOUS_LIST

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 09-10-2015
hi, i have implemented the script but it is not renaming the files. i have copied the files in
sms_tmp but when i run the script it is not renaming the files with sequence number also file is not transfered to sms dir. in previous_file_list i have the record of files which are in sms_tmp dir while file_list and squence_file is empty
# 6  
Old 09-11-2015
Hello mfaizan40,

I have created a same directory structure in my /tmp and executed the following(previous script with minor change), it worked for me.
Code:
cat check_move_files.ksh
ACTUAL_PATH=/tmp/home/billing/Cmm/sms
TMP_PATH=/tmp/home/billing/Cmm/sms/sms_tmp
SEQ_FILE=/tmp/home/billing/Cmm/sms/squence_file.txt
LIST_FILES=/tmp/home/billing/Cmm/sms/files_list
PREVIOUS_LIST=/tmp/home/billing/Cmm/sms/previous_file_list
touch $SEQ_FILE $LIST_FILES
FILES_NEEDS_TO_BE_CREATED=/tmp/home/billing/Cmm/sms/files_needs_to_be_created
find $TMP_PATH -type f -printf "%f\n" 2>/dev/null > $LIST_FILES
VAL=`cat $SEQ_FILE`
VAL1=printf "%05d\n" $VAL
ONE=1
MAKE_CHECK_FILE() {
Input_file=$1
        while read line
        do
                VAL=`expr $VAL + $ONE`
                VAL1=`printf "%05d\n" $VAL`
                mv $TMP_PATH"/"$line $ACTUAL_PATH"/"$line"_"$VAL1
        done < $Input_file
                    }
 
if [[ -s $PREVIOUS_LIST ]]
then
        awk 'FNR==NR{sub(/\_.*/,X,$0);A[$0];next} !($0 in A){print $0}' $PREVIOUS_LIST $LIST_FILES > $FILES_NEEDS_TO_BE_CREATED
        MAKE_CHECK_FILE $FILES_NEEDS_TO_BE_CREATED
else
        MAKE_CHECK_FILE $LIST_FILES
fi
 
printf "%05d" $VAL1 > $SEQ_FILE
mv $LIST_FILES $PREVIOUS_LIST

Please remove /tmp from solution and try it, let me know if you have any queries. Please test all scenarios like first create some files and run it and then create some same name files and try to run script it shouldn't move already existing ones and move the new ones.

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-11-2015 at 03:55 AM.. Reason: Removed Val1 and Val2 values from post now, thanks to wisecracker
# 7  
Old 09-11-2015
Hi RavinderSingh13...

Where has $VAL2, (line 19), come from?
Code:
                echo $VAL1 $VAL2

It will always be a NULL...
If I am wrong I stand corrected...
This User Gave Thanks to wisecracker For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To check the missing file based on sequence number.

Hi All, I have a requirement that i need to list only the missing sequences with a unix script. For Example: Input: FILE_001.txt FILE_002.txt FILE_005.txt FILE_006.txt FILE_008.txt FILE_009.txt FILE_010.txt FILE_014.txt Output: FILE_003.txt FILE_004.txt FILE_007.txt FILE_011.txt... (5 Replies)
Discussion started by: Arun1992
5 Replies

2. Shell Programming and Scripting

Appending sequence number at the end of file name

hi team, i need a script for renaming a file with sequence number. script get a file from one directory'/home/billing/Cmm/sms/sms_tmp' append sequence no at the end of file name and move a file to other directory/home/billing/Cmm/sms/. actual file is cdr201508271527 file after renaming ... (3 Replies)
Discussion started by: mfaizan40
3 Replies

3. Shell Programming and Scripting

Need Help in adding sequence number to a file

Hi All , I have a file which contains data(comma separated) in below format : 500,Sourav ,kolkata ,8745775020,700091 505,ram,delhi ,9875645874,600032 510 ,madhu ,mumbai ,5698756430 ,500042 515 ,ramesh ,blore ,8769045601 ,400092 I want to add unique sequence number at the start of each... (7 Replies)
Discussion started by: STCET22
7 Replies

4. Shell Programming and Scripting

Renaming the file name for n number of files

Hi , I am kind of new to shell scripting and found a situation to handle ... I have few files which will be ftpd in to our sustem , the file names needs to be renamed based on condition. ------------ Eg file names :- AE_JUNFOR_2013_MTD_2013-04-09-08-30-09.TXT... (6 Replies)
Discussion started by: chillblue
6 Replies

5. Shell Programming and Scripting

find common entries and match the number with long sequence and cut that sequence in output

Hi all, I have a file like this ID 3BP5L_HUMAN Reviewed; 393 AA. AC Q7L8J4; Q96FI5; Q9BQH8; Q9C0E3; DT 05-FEB-2008, integrated into UniProtKB/Swiss-Prot. DT 05-JUL-2004, sequence version 1. DT 05-SEP-2012, entry version 71. FT COILED 59 140 ... (1 Reply)
Discussion started by: manigrover
1 Replies

6. Shell Programming and Scripting

How to insert a sequence number column inside a pipe delimited csv file using shell scripting?

Hi All, I need a shell script which could insert a sequence number column inside a dat file(pipe delimited). I have the dat file similar to the one as shown below.. |A|B|C||D|E |F|G|H||I|J |K|L|M||N|O |P|Q|R||S|T As shown above, the column 4 is currently blank and i need to insert sequence... (5 Replies)
Discussion started by: nithins007
5 Replies

7. UNIX for Dummies Questions & Answers

Adding Sequence Number to file

Hi All, I need to create a script which checks for a particular file for eg.kumar1.txt. If kumar1.txt is already exist the script should increment the file name as kumar2.txt and so on. Please Advise. Thanks & Regards, Kumar66 (2 Replies)
Discussion started by: kumar66
2 Replies

8. Shell Programming and Scripting

How to split a file with adding sequence number and extension.

I have a file name -HRCFTSIN05PLA1602100430444444 my requirement is to split the file in 10000 count each file and to add sequence number.rch at the end of each file. output should be in this format HRCFTSIN05PLA160210043044444401.rch HRCFTSIN05PLA160210043044444402.rch... (4 Replies)
Discussion started by: abhigrkist
4 Replies

9. Shell Programming and Scripting

Renaming a file use another file as a sequence calling a shl

have this shl that will FTP a file from the a directory in windows to UNIX, It get the name of the file stored in this variable $UpLoadFileName then put in the local directory LocalDir="${MPATH}/xxxxx/dat_files" that part seems to be working, but then I need to take that file and rename, I am using... (3 Replies)
Discussion started by: rechever
3 Replies

10. Shell Programming and Scripting

Adding a sequence number within a file

Can someone please help. I need to add a sequence number to the start of each line of a file. It needs to be 6 characters long. ie 000001 next line starts 000002 etc. (4 Replies)
Discussion started by: Dolph
4 Replies
Login or Register to Ask a Question