Incrementing the New File Version Number


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Incrementing the New File Version Number
# 1  
Old 06-12-2013
Incrementing the New File Version Number

Hello All,
In the below script i am trying to check and list the file names, get the last file with highest version number and then increment the version number when i create another file. Example: file1 is COBANK_v1.xml and file2 i want to put it as COBANK_v2.xml, to achieve this i am using awk and want to do an expression later to increment the number. Can you please help.

Code:
#!/bin/bash
. /home/infrmtca/bin/dwh_Loadfuncs.sh
PROVIDER=$1
case $PROVIDER in
        1)      FILENAME="AAC";;
        4)      FILENAME="AGFIRST";;
        5)      FILENAME="AGRIBANK";;
        6)      FILENAME="COBANK";;
        7)      FILENAME="COLUSA-GLENN";;
        8)      FILENAME="FCSA";;
        11)     FILENAME="HAWAII";;
        12)     FILENAME="IDAHO";;
        16)     FILENAME="FCBTEXAS";;
        17)     FILENAME="WAC";;
        18)     FILENAME="AGVANTIS";;
        20)     FILENAME="YOSEMITE";;
        21)     FILENAME="FPI";;
        *)      FILENAME="Not Valid";;
esac
echo $FILENAME

if [ -f "$TGT_DIR"/"$FILENAME"*.xml ]
then
        echo "success"
        for i in "$TGT_DIR"/"$FILENAME"*.xml
        do
                basename "$i" | awk -F"." '{print $2}'
        done
else
        echo "Failure"
fi

Thank you.
# 2  
Old 06-12-2013
You can run them through sed to create a shell command:
Code:
sed '
    s/\(.*_v\)\([1-9][0-9]*\)\(.*\)/echo "\1$(( \2 + 1 ))\3"/
  ' | bash

# 3  
Old 06-12-2013
i think i got my script working, there might be some over kills in my coding, please let me know if you have any suggestions in regards to my code.

Code:
#!/bin/bash
. /home/infrmtca/bin/dwh_Loadfuncs.sh
PROVIDER=$1
case $PROVIDER in
        1)      FILENAME="AAC";;
        4)      FILENAME="AGFIRST";;
        5)      FILENAME="AGRIBANK";;
        6)      FILENAME="COBANK";;
        7)      FILENAME="COLUSA-GLENN";;
        8)      FILENAME="FCSA";;
        11)     FILENAME="HAWAII";;
        12)     FILENAME="IDAHO";;
        16)     FILENAME="FCBTEXAS";;
        17)     FILENAME="WAC";;
        18)     FILENAME="AGVANTIS";;
        20)     FILENAME="YOSEMITE";;
        21)     FILENAME="FPI";;
        *)      FILENAME="Not Valid";;
esac
echo "Supplied Input FileName: "$FILENAME

for i in `ls -lt "$TGT_DIR"/"$FILENAME"*.xml | awk '{print $9}' | head -1`
do
        FILE_NAME=`basename $i | awk -F"v" '{print $1}'`
        VER_NUM=`basename "$i" | awk -F"v" '{print $2}' | cut -d"." -f1`
done
echo "First Part Of New File Name: "$FILE_NAME
echo "Versioin Of Current File: "$VER_NUM

INC_VER_NUM=$[$VER_NUM+1]
NEW_FILE_NAME="$FILE_NAME"v"$INC_VER_NUM".xml

echo "New Versioned File Name: "$NEW_FILE_NAME

# 4  
Old 06-13-2013
Avoiding external tools viz. basename, cut, awk and minimising the use of pipes
Code:
for i in `ls -t "$TGT_DIR"/"$FILENAME"*.xml | head -1`
do
    file=${i##*/}
    FILE_NAME=${file%v*}
    t=${file%.*}
    VER_NUM=${t#*v}
done


Last edited by balajesuri; 06-13-2013 at 01:53 AM..
# 5  
Old 06-13-2013
Quote:
Originally Posted by balajesuri
Avoiding external tools viz. basename, cut, awk and minimising the use of pipes
Code:
for i in `ls -t "$TGT_DIR"/"$FILENAME"*.xml | head -1`
do
    file=${i##*/}
    FILE_NAME=${file%v*}
    t=${file%.*}
    VER_NUM=${t#*v}
done

Even this uses ls and head and makes the wild assumption that the latest version of any Provider's files will be the one with the most recent timestamp. It also assumes that there will be at least one existing file for each provider. The following script is much more complex than balajesuri's script but will create version 1 of a file for a provider that doesn't currently have any files, will use the highest numbered version rather than the last touched version of a Provider's files if more than one exists, and will print a diagnostic and exit if no provider code is specified or if an unknown provider code is specified:
Code:
#!/bin/bash
IAm=${0##*/}    # Last component of script name for diagnostics
Usage='Usage: %s Provider_Code
        Provider_Code "%s" unknown
Valid Provider_Code values are:
Provider_Code   Provider
=============   ============
         1      AAC
         4      AGFIRST
         5      AGRIBANK
         6      COBANK
         7      COLUSA-GLENN
         8      FCSA
        11      HAWAII
        12      IDAHO
        16      FCBTEXAS
        17      WAC
        18      AGVANTIS
        20      YOSEMITE
        21      FPI
'
. /home/infrmtca/bin/dwh_Loadfuncs.sh
#TGT_DIR=.       # Assume that TGT_DIR is defined by above sourced script.
case "$1" in
        (1)     FILENAME="AAC";;
        (4)     FILENAME="AGFIRST";;
        (5)     FILENAME="AGRIBANK";;
        (6)     FILENAME="COBANK";;
        (7)     FILENAME="COLUSA-GLENN";;
        (8)     FILENAME="FCSA";;
        (11)    FILENAME="HAWAII";;
        (12)    FILENAME="IDAHO";;
        (16)    FILENAME="FCBTEXAS";;
        (17)    FILENAME="WAC";;
        (18)    FILENAME="AGVANTIS";;
        (20)    FILENAME="YOSEMITE";;
        (21)    FILENAME="FPI";;
        (*)     printf "$Usage" "$IAm" "$1" >&2
                exit 1;;
esac
echo "Supplied Input FileName: $FILENAME"

MAX_V=0 # Set highest version seen for the Provider specified by $FILENAME.
for i in "$TGT_DIR/$FILENAME"*.xml 
do      if [ "$MAX_V" == 0 ] && [ "$i" != "${i#*[*]}" ]
        then    # No matches were found, set up to create 1st file for this
                # Provider.  ($FILENAME and $MAX_V are already set
                # appropriately, so just get out.)
                break
        fi
        # Strip off $TGT_DIR/$FILENAME and "_v"
        VER_NUM=${i##*v}
        # Strip off the .xml to get just the version number.
        VER_NUM=${VER_NUM%.*}
        # If this version number is > than previously seen versions, save it.
        # This make the assumption that the version # will be numeric; if a
        # user might create a file with a clashing unexpected name, the code
        # should be extended to verify that $VER_NUM is a numeric string.
        [ "$VER_NUM" -gt "$MAX_V" ] && MAX_V=$VER_NUM
done
echo "First Part Of New File Name: $FILENAME"
echo "Versioin Of Current File: $MAX_V"
NEW_FILE_NAME="${FILENAME}_v$((MAX_V + 1)).xml"
echo "New Versioned File Name: "$NEW_FILE_NAME

Note that although this script uses $!/bin/bash, this code will work with any POSIX conforming shell (including, but not limited to, bash and ksh) as long as nothing bash specific appears in the sourced script.

Last edited by Don Cragun; 06-13-2013 at 04:33 AM.. Reason: Fix autocorrection indueced typos...
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 06-13-2013
Fear of pipes can and should be overcome! But some tools like basename are silly, dated, when you can just ${full_path##*/} and save a fork() and exec().
# 7  
Old 06-13-2013
Thanks Don Awesome!!, i tested the script it's working fine with no single error. How ever i have few questions.

1) Is "${i#*[*]}" same as "${i#**}" when i tested it is giving same results, may i please know the purpose of those square brackets then, additional check or something?

2) [ "$VER_NUM" -gt "$MAX_V" ] && MAX_V=$VER_NUM
Here it will check if VER_NUM is greater than previous version and if it is then save it to MAX_V, probably another syntax or way of assigning a value to variable.

3) IAm=${0##*/} --- Trying to debug this but don't understand completely. As per my assumption you were using this in conjunction with printf to print the output? may i please know what does each of those${0##*/} represent?

4) printf "$Usage" "$IAm" "$1" >&2 --- i know you are redirecting to standard output, and you are passing Usage & IAm variables along with input argument(provider_code). Much appreciate if you could explain a bit.

Thank you.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX replacing and incrementing number

Hi I am unix newbie looking for a unix bash script that can make it easier to do my code work. we have a code number for each code block that we want to incrementally assign. We have 10000 of these and it is very laborious to do this one by one. so what we want is start from the top of the... (4 Replies)
Discussion started by: chamajid
4 Replies

2. UNIX for Beginners Questions & Answers

Incrementing the New File Version Number

Hi, This is my first post here. I am using cygwin on Windows 7. I am starting with a data file with filename "name_1.ext", like "20180831_snapgenotypes_1.csv". The "_1" before ".ext" is a version number. Integers (0-99) are sufficient. They don't have to be like "1.0.0". The filename may... (2 Replies)
Discussion started by: minimalist
2 Replies

3. UNIX for Advanced & Expert Users

Add an incrementing identity column to a file

Two questions: 1) Is there a way to create a new column (i.e. ID) to an existing file containing data and the new column ID populate as an auto incrementing number? for example: (Current file has headers and is PIPE delimited.) **data looks like this** "COL1"|"COL2"|"COL3"|"COL4"|"COL5"... (4 Replies)
Discussion started by: timlisa20
4 Replies

4. UNIX for Advanced & Expert Users

Add an incrementing identity column to a file

Two questions: 1) Is there a way to create a new column (i.e. ID) to an existing file containing data and the new column ID populate as an auto incrementing number? for example: (Current file has headers and is PIPE delimited.) **data looks like this** "COL1"|"COL2"|"COL3"|"COL4"|"COL5"... (0 Replies)
Discussion started by: timlisa20
0 Replies

5. Shell Programming and Scripting

Incrementing parts of ten digits number by parts

I have number in file which contains date and serial number: 2013101000. The last two digits are serial number (00). So maximum of serial number is 100. After reaching 100 it becomes 00 with incrementing 10 which is day with max 31. after reaching 31 it becomes 00 and increments 10... (31 Replies)
Discussion started by: Natalie
31 Replies

6. Shell Programming and Scripting

Replacing string by incrementing number

Dear all Say I have a file as ! TICKET NBR : 234 !GSI : 102 ! 3100.2.112.1 11/06/2013 15:56:29 ! 3100.2.22.3 98 ! 3100.2.134.2 8 ! ! TICKET NBR : 1809 ! GSI : 102 ! 3100.2.112.1 11/06/2013 16:00:45 ! 3100.2.22.3 65 ! 3100.2.134.2 3 ! ! TICKET NBR : 587 ! GSI : 102 ! 3100.2.112.1... (3 Replies)
Discussion started by: OTNA
3 Replies

7. Shell Programming and Scripting

Change numbers in a file, incrementing them

Hello, Here's a file of mine: key1:431 key2:159 key3:998 I need to change these keys to something bigger - and I actually need to shift them all by a range of 3. The output would be: key1:434 key2:162 key3:1001 I can't find the propper sed/awk line that would alter all my... (4 Replies)
Discussion started by: fzd
4 Replies

8. Shell Programming and Scripting

Incrementing number in bash

I have the following code and getting the error ./raytrac.bash: line 231: ((: 0++: syntax error: operand expected (error token is "+") iarg = 0 iarg=0 narg=$# # Number of arguments passed. echo "narg = $narg" argsArr=("$@") # Set... (1 Reply)
Discussion started by: kristinu
1 Replies

9. Shell Programming and Scripting

perl + array and incrementing number

morning guys and gals, I am haveing a problem, a friend helped me out with this script but i dont know how to add incrementing number for each movie in movie.list. this is what i have so far. any assistance would be great. I have removed the GT and LT symbols so you can see what is going on... (5 Replies)
Discussion started by: Optimus_P
5 Replies

10. Shell Programming and Scripting

Port incrementing using one file

Hi I wrote this script with the help of you guyz.My next challenge is to increment the port using single file.So far iam using this code to increment hp1 and hp2 .To increment port numbers, iam using two different files (.default_port_hp1 and .default_port_hp2).The challenge for me is to use... (0 Replies)
Discussion started by: coolkid
0 Replies
Login or Register to Ask a Question