Incrementing the New File Version Number


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Incrementing the New File Version Number
# 1  
Old 09-13-2018
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 contain other "_" or "-" characters.

The filename might not have a version number, as in "name.ext" or "20180831_snapgenotypes.csv".

I want to leave the current filename and the contents of the file intact. I want to do something (or nothing) to the file contents and save the output to a new file of the same name but increment the version number by 1.

"name.ext" should become "name_1.ext".
"name_1.ext" should become "name_2.ext".
and so on.

I found this post "unix-for-dummies-questions-and-answers/228027-incrementing-new-file-version-number.html" and wrote:

Code:
#!/bin/bash

infile=20180831_snapgenotypes_1.csv

echo "infile: "$infile

name=${infile%_*} # Strip off the last "_" and everything after it.
version=${infile##*_} # Strip off the name and "_".
version=${version%.*} # Strip off the .ext to get just the extension.
extension=${infile##*.} # Strip off everything before ext.

outfile="${name}_$((version + 1)).$extension"
echo "outfile: "$outfile

This seems to work if my infile has a version number "_nn".

I tried to add a test whether the filename has this version number and, if not, name the outfile "name_1.ext". My echo statements are just for debugging:

Code:
#!/bin/bash

infile=$1

echo "infile: "$infile

name=${infile%.*} # Strip off the extension.
echo '${name} =' "${name}"

extension=${infile##*.} # Strip off everything before ext.
echo '${extension} =' "${extension}"

version_dot=${infile##*_} # Strip off the name and "_".
echo '${version_dot} =' "${version_dot}"

version=${version_dot%.*} # Strip off the extension.
echo '${version} =' "${version}"

if [ ${version} != [[:digit:]]] # This condition is not working.
then ${infile}="${name}_1.${extension}"
else name=${infile%_*} # Strip off the last "_" and extension.
fi

echo '${name} =' "${name}"
outfile="${name}_$((version + 1)).${extension}"
echo "outfile: "$outfile

My test

Code:
if [ ${version} != [[:digit:]]] # If the "version number" is not a (1 - 2 digit) number, but text.

is not correct. It always evaluates to false.
There is an error from this line: [: missing `]'. I don't get that clue.


Infile 20180831_snapgenotypes.csv becomes outfile 20180831_1.csv.
Infile 20180831_snapgenotypes_1.csv becomes outfile 20180831_snapgenotypes_2.csv (correct).

I am trying to make this as simple as possible, but a call to sed, AWK, or Perl would be ok.

Thank you!
# 2  
Old 09-14-2018
Hi,
Try to replace:
Code:
if [ ${version} != [[:digit:]]]

by (space is important) :
Code:
if [[ ${version} =~ ^[[:digit:]]+$ ]]

But, you must sort your file list, because if you rename file_1 to file_2 before file_2 to file_3, you will rename file_1 to file_3 and real file_2 will suppress.

Regards.
# 3  
Old 09-14-2018
Thank you, this worked!
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 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

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"... (0 Replies)
Discussion started by: timlisa20
0 Replies

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

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. UNIX for Dummies Questions & Answers

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... (15 Replies)
Discussion started by: Ariean
15 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