Sponsored Content
Top Forums Shell Programming and Scripting Shell Scripts (Renaming file names with sequential numbers) Post 302527950 by thebeno on Monday 6th of June 2011 06:11:06 AM
Old 06-06-2011
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 04:26 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy