Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Incrementing the New File Version Number Post 302820591 by Don Cragun on Thursday 13th of June 2013 03:31:47 AM
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:
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

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