Shell Scripts (Renaming file names with sequential numbers)


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Scripts (Renaming file names with sequential numbers)
# 1  
Shell Scripts (Renaming file names with sequential numbers)

Hi there,

Firstly, I have no experience with shell scripts so would really appreciate some help.

I have the following shell script that is causing some problems:

Code:
moveit()
{
    [ ${DEBG} = "1" ] && set -x

    if [ -f ${JOURNAL}.${ASOF_DATE} ]  
    then
        DOUBLE_DELIVERY=$(grep "${SW_ID}.*${P_DIR}.*${TARGET_NAME}.*${N_OF_REC}.*${SZ_KI}" ${JOURNAL}.${ASOF_DATE})
    fi
    if [ ! -z $DOUBLE_DELIVERY ]
    then
        LOC_TAG="ASIFUNC_01"
        ERROR_MSG="File $2 was sent already" 
        msg_error ${ERROR_MSG} && temp_err_handler FI_DBLE
    fi

    LOC_TAG="ASIFUNC_02" 
    ERROR_MSG="Destination $2 already exists" 
    ls "$2" >/dev/null 2>&1 && msg_error ${ERROR_MSG} && temp_err_handler FI_EXST
    msg_message "\"$1\" has been moved to \"$2\""
    mv "$1" "$2"
    if [ "$?" -ne "0" ] 
    then
        LOC_TAG="ASIFUNC_03"
        ERROR_MSG="Can't move file $1 to $2"
        msg_error ${ERROR_MSG}
        temp_err_handler MV_FILE
    else
        chmod 660 $2
        chgrp ggearch $2
        STATUS_MSG="OK"

        USEFUL_VALUE=`echo $ASOF_DATE | grep '^20..[0-1][0-9][0-3][0-9]'` 
        if [ ! -z $USEFUL_VALUE ]
        then
        if [ ! -f ${JOURNAL}.${ASOF_DATE} ] 
            then
                createFile ${JOURNAL}.${ASOF_DATE} 
            fi
            echo "${SW_ID} ${P_DIR} ${TARGET_NAME} ${N_OF_REC} ${SZ_KI} ${NOW} ${SCRIPT_INDX} ${P_ASIFTYP}">> ${JOURNAL}.${ASOF_DATE}
        else
            msg_message "Could not generate a Journal file for $2"
        fi
        logStatus
    fi
}
#

The problem is, the current setup is causing problems when we have duplicate archiving retrievals, as I can see the log file being written does not allow duplicate entries with the same. I need to change this by creating a sequential number and renaming the file with say _1, then the next file can be called _2. This would enable us to retrieve an archiving period as many times as we need to without the errors.

I would propose to delete the first statement:
Code:
if [ -f ${JOURNAL}.${ASOF_DATE} ] 
    then
        DOUBLE_DELIVERY=$(grep "${SW_ID}.*${P_DIR}.*${TARGET_NAME}.*${N_OF_REC}.*${SZ_KI}" ${JOURNAL}.${ASOF_DATE})
    fi
    if [ ! -z $DOUBLE_DELIVERY ]
    then
        LOC_TAG="ASIFUNC_01"
        ERROR_MSG="File $2 was sent already" 
        msg_error ${ERROR_MSG} && temp_err_handler FI_DBLE
    fi

then I would place the command in the lower part of the code. However, I am unsure of any shell commands, I have written in bold below the logical explanation of what I want to achieve, if there's someone that can help with the code it would be greatly appreciated.

Code:
else
        chmod 660 $2
        chgrp ggearch $2
        STATUS_MSG="OK"

        USEFUL_VALUE=`echo $ASOF_DATE | grep '^20..[0-1][0-9][0-3][0-9]'` 
        if [ ! -z $USEFUL_VALUE ]
        then
        if [ ! -f ${JOURNAL}.${ASOF_DATE} ] 
            then
                createFile ${JOURNAL}.${ASOF_DATE}._1 
            fi
            echo "${SW_ID} ${P_DIR} ${TARGET_NAME} ${N_OF_REC} ${SZ_KI} ${NOW} ${SCRIPT_INDX} ${P_ASIFTYP}">> ${JOURNAL}.${ASOF_DATE}
        else

if file name ends in "_1"
then rename file (_1 + 1) i.e. _2
if file name ends in "_2"
then rename file (_2 + 1) i.e. _3 and so on.....  
end 

            msg_message "Could not generate a Journal file for $2" 
        fi
        logStatus
    fi


Last edited by Franklin52; 06-06-2011 at 07:15 AM.. Reason: Please use code tags
# 2  
Code:
in your code itself u can declare variable like this.
n=$(( a + 1 ))
here a is the value that you are defining like _1 or _2 etc....so the same can be increamented and then use in mv oldfile(without _1) to newfile_"$n"

Thanks
SHa
# 3  
Thanks for your help.

So my code would be: (it still needs some work :-) )

Code:
else
        chmod 660 $2
        chgrp ggearch $2
        STATUS_MSG="OK"

        USEFUL_VALUE=`echo $ASOF_DATE | grep '^20..[0-1][0-9][0-3][0-9]'` 
        if [ ! -z $USEFUL_VALUE ]
        then
        if [ ! -f ${JOURNAL}.${ASOF_DATE} ] 
            then
                createFile ${JOURNAL}.${ASOF_DATE}.${_}
.${n}
where n=$(( a + 1 ))

            fi
            echo "${SW_ID} ${P_DIR} ${TARGET_NAME} ${N_OF_REC} ${SZ_KI} ${NOW} ${SCRIPT_INDX} ${P_ASIFTYP}">> ${JOURNAL}.${ASOF_DATE}
        else

if file name ends in "_1"
then mv oldfile(${JOURNAL}.${ASOF_DATE}.${_}.${n} 
to newfile_"$n"
end 

            msg_message "Could not generate a Journal file for $2" 
        fi
        logStatus
    fi

# 4  
I am afraid I do not get hold of your code completely.

Given sample shell script with same kind of logice hope this should give you better understanding..

sample shell script which might help you.
Below are the list of files which I want to just rename by increamenting the numbers with already exist files.
Code:
 
filename_1
filename_3
filename_5

Code:
 
for f in `ls -lrth file*|nawk '{print $NF}'`
do
file=`echo $f|nawk -F"_" '{print $1}'` # store filename without _[0-9] numbers
a=`echo $f|nawk -F"_" '{print $NF}'` store variable which I want to increment by 1
n=$(( a + 1 ))   
mv $f $file_"$n"  # renaming file with new _numbers[0-9]
done

Output will be

Code:
 
filename_2
filename_4
filename_6

Thanks
Sha
# 5  
@Shahul
It's simpler:
Code:
for f in *[0-9]; do
  mv "$f" "`echo "$f" | awk -F'_' '{print $1 "_" $2+1}'`"
done

# 6  
Thanks guys, can you help with the initial naming of the first file to ****_1


Code:
createFile ${JOURNAL}.${ASOF_DATE}

# 7  
The code given by both should work and still Yep I shall advice you to go ahead with yazu format ...that should also work for the first file as well...

Smilie
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #619
Difficulty: Medium
In Python, variables may not be used by just assigning them a value. Declaration and data type definition is required in Python.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Renaming the file names in a directory

Hi, I have about 60 files in a directory and need to rename those files. For example the file names are i_can_phone_yymmdd.txt (where yymmdd is the date. i.e 170420 etc) i_usa_phone_1_yymmdd.txt i_eng_phone_4_yymmdd.txt The new file names should be phone.txt phone_1.txt phone_4.txt I am... (4 Replies)
Discussion started by: naveed
4 Replies

2. Shell Programming and Scripting

Renaming File Names in a folder/Dir

Hi Team, I'm new to Unix shell scripting . I've the following requirement A folder contains the list of files with the following format ab.name.11.first ab.name.12.second ab.name.13.third ---------- I have to rename the above file to like below ... (6 Replies)
Discussion started by: smile689
6 Replies

3. Shell Programming and Scripting

Sequential numbers

Hi All, I am looking for a simple way to write numbers to a file sequentially starting from 1 and ending on a specified upper limit. Example of the output file is below Example 1 2 3 4 5 . . . . 1000 please let me know the best way to do it. (10 Replies)
Discussion started by: Lucky Ali
10 Replies

4. UNIX for Dummies Questions & Answers

Renaming files in one file from names in other

Hi Guys, I have a small problem of renaming multiple files. For example I have names of a set of files in one directory like K2_34625-34675 K7_988963-988983 K12_773882-7734102 and the other set corresponding to the same is U_P_321_9_3_11.ab1 U_P_322_9_3_11.ab1 U_P_323_9_3_11.ab1 Now... (23 Replies)
Discussion started by: pawannoel
23 Replies

5. Shell Programming and Scripting

Renaming file names

I have 7 files with 7 different names coming into a specified folder on weekly basis, i need to pick a file one after another and load into oracle table using sql loader. I am using ksh to do this. So in the process if the file has error records and if sql loader fails to load into oracle tables,... (2 Replies)
Discussion started by: vpv0002
2 Replies

6. Shell Programming and Scripting

Renaming file names in a shell script

I want to write a shell script that will rename all the file names to today's date attached to it.. so for example i have a file names like file1.sales.20081201.txt.c zbrs.salestxtn.20091101.txt.inn then it will rename both the files with todays date to it so the file names get changed... (1 Reply)
Discussion started by: rudoraj
1 Replies

7. Shell Programming and Scripting

Finding missing sequential file names

So, I've got a ton of files that I want to go through (ie something like 300,000), and they're all labeled sequentially. However I'm not 100% positive that they are all there. Is there any way of running through a sequence of numbers, checking if the file is in the folder, if not appending it... (2 Replies)
Discussion started by: Julolidine
2 Replies

8. UNIX for Dummies Questions & Answers

how to separate numbers and words from a file using shell scripts

Hi, How to separate numbers and words(with full alphabets) in a particular file and store it in two different files. Please help me out for this.Using shell scripting. :confused::confused: (1 Reply)
Discussion started by: kamakshi s
1 Replies

9. Programming

Reading special characters while converting sequential file to line sequential

We have to convert a sequential file to a 80 char line sequential file (HP UX platform).The sequential file contains special characters. which after conversion of the file to line sequential are getting coverted into "new line" or "tab" and file is getting distorted. Is there any way to read these... (2 Replies)
Discussion started by: Rajeshsu
2 Replies

10. UNIX for Dummies Questions & Answers

inserting uniq sequential numbers at the start of the file

Hi Unix gurus, I have a file. I need to insert sequential number at the starting of the file. Fields are delimited by "|". I know the starting number. Example: File is as follows |123|4test|test |121|2test|test |x12|1test|test |vd123|5test|test starting number is : 120 ... (7 Replies)
Discussion started by: jingi1234
7 Replies

Featured Tech Videos