Incremental numbering?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Incremental numbering?
# 1  
Old 01-29-2013
Incremental numbering?

Would it be possible for a script to duplicate a file and incrementally number it?

File in: XXX_007_0580_xxxx_v0016.aep
File out: XXX_007_0580_xxxx_v0017.aep

If someone knows of a way I'd love to see it.

Thanks!
# 2  
Old 01-29-2013
Is it always _v####?

Code:
mute@clt:~/temp/scribling$ ./script
cp XXX_007_0580_xxxx_v0016.aep new/XXX_007_0580_xxxx_0017.aep

Code:
#!/bin/sh

for file in *.aep; do
        ext=${file##*.}
        base=${file%.*}
        num=${base##*v}
        zeroes=${num%%[!0]*}
        num=${num#$zeroes}      #remove leading zeros, or it uses octal
        num=$((num+1))
        base=${base%v*}
        new=$(printf '%s%04d.%s' "$base" "$num" "$ext")
        echo cp "$file" "new/$new"
done

# 3  
Old 01-29-2013
It would help to know which shell you are using.

The most likely shells - ksh and bash - both can do this the following way:

First, extract from the file name the part you want to increment as a number,
Second, increment the isolated number,
Third: reconstruct the new filename with the number.

For instance, in ksh:

Code:
typeset    infile="XXX_007_0580_xxxx_v0016.aep"
typeset -i num=0
typeset    outfile=""
typeset    work=""

work="${infile%.???}"     # cut off extension
num=${work#?????????????????????}      # cut off anything before "16"

(( num += 1 ))                  # increment number
outfile="XXX_007_0580_xxxx_v00${num}.eap"   # reconstruct file name

You probably got the gist of this and can adapt this procedure to your needs.

I hope this helps.

bakunin
# 4  
Old 01-29-2013
Neutronscott's solution is better as is supports any length string before the version and num values above 99

one enhancement is to deal with octal value using [icode[[bash#]num[/icode] syntax of ksh/bash, ie replace:
Code:
        zeroes=${num%%[!0]*}
        num=${num#$zeroes}      #remove leading zeros, or it uses octal
        num=$((num+1))

with:
Code:
    num=$((10#$num + 1))

# 5  
Old 01-30-2013
How about using BASH
Code:
#!/bin/bash
for file in XXX_007_0580_xxxx_v00{16..17}.aep
do
      echo $file
done

# 6  
Old 01-30-2013
Quote:
Originally Posted by neutronscott
Is it always _v####?

Code:
mute@clt:~/temp/scribling$ ./script
cp XXX_007_0580_xxxx_v0016.aep new/XXX_007_0580_xxxx_0017.aep

Code:
#!/bin/sh

for file in *.aep; do
        ext=${file##*.}
        base=${file%.*}
        num=${base##*v}
        zeroes=${num%%[!0]*}
        num=${num#$zeroes}      #remove leading zeros, or it uses octal
        num=$((num+1))
        base=${base%v*}
        new=$(printf '%s%04d.%s' "$base" "$num" "$ext")
        echo cp "$file" "new/$new"
done

So close...
This script stripped out the period (.) in the extension when setting ext and when setting base, but puts it back in the printf format string. It also strips out the vee (v) when setting num and when setting base, but it doesn't put it back. I think the first argument to printf in this script needs to be changed from '%s%04d.%s' to '%sv%04d.%s'.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 01-30-2013
Yes, so close ...

Well, it works great on the input file but unfortunately some other .aep files in the directory also get incremented.

I've altered it to work.
Code:
#!/bin/sh

for file in "$@"
do
        ext=${file##*.}
        base=${file%.*}
        num=${base##*v}
        zeroes=${num%%[!0]*}
        num=${num#$zeroes}      #remove leading zeros, or it uses octal
        num=$((num+1))
        base=${base%v*}
        new=$(printf '%sv%04d.%s' "$base" "$num" "$ext")
        cp -nv "$file" "$new"
done

I wrote it into an Automator Service script.

Thank you!
This is truly sweet.

---------- Post updated at 10:46 PM ---------- Previous update was at 10:30 PM ----------

Is there a way other than "cp -n" to make sure it doesn't overwrite an existing file?

If accidentally run on ...v0016 and ...v0017 already exists I just want it to fail rather than create an incorrect ...v0018. Not that it does that now, but if v0018 was created from a copy of ...v0016 it would be incorrect.

Thanks.

Last edited by scribling; 01-30-2013 at 02:36 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Numbering by field

I'm not really sure how to explain this but I will try. In the attached file if $4=$4 and $5="-" then the last record is 1 and the one above that is 2, etc... However, $4=$4 and $5="-" then the first record is 1 and the one below that is 2, etc... "-" example: chr10 90694830 90695123... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

help with numbering a file

Hi, All I need to do is number a file. The file looks like this > JJJJJJJJJJJJJJJJJJJJJ > JKJKJKKKKKKJJJ > MMMMYKKKJKKK what I want to do is number it so that theres a numerical value beside the >. >1 JJJJJJJJJJJJJJJJJJJJJ >2 JKJKJKKKKKKJJJ (2 Replies)
Discussion started by: kylle345
2 Replies

3. UNIX for Dummies Questions & Answers

incremental by 1

let says, i have this number as 000002080, i want to add 1 to make it 000002081, and then i want to add 1 to 000002082, add 1 to 000002083, 84. i=000002080 TOT=$(echo "scale=9; $i + 1" | bc) echo $TOT it shows 2081, i want to retain 000002081, 000002082, 000002082, 000002084. (2 Replies)
Discussion started by: tjmannonline
2 Replies

4. UNIX for Dummies Questions & Answers

Numbering the rows

If I a list of components, is there anyway to number (like automatically have: 1,2,3,...) the rows of my data? (1 Reply)
Discussion started by: cosmologist
1 Replies

5. Shell Programming and Scripting

Numbering duplicates

Hi, I have this large file and sometimes there are duplicates and I want to basically find them and figure how many there are. So I have a file with multiple columns and the last column (9) has the duplicates. eg. yan tar tar man ban tan tub tub tub Basically what I want to... (6 Replies)
Discussion started by: kylle345
6 Replies

6. Shell Programming and Scripting

Numbering Lines

Hello everyone, I want get numbered lines from a file. and i can do it with: sed = file.txt | sed "/./N; s/\n/ /" | sed -n "5,7p" but the output that i get is something similar to: 5 line5 6 line6 7 line7 and i want something like this (with 2points after the number): 5:... (6 Replies)
Discussion started by: vibra
6 Replies

7. UNIX for Advanced & Expert Users

numbering blanks

hello i'm trying to figure out how to number a blank line. For instance this : sed '/./=' file | sed '/./N; s/\n/ /' gives me 1 aaaa 2 bbbbbb 4 cccccc 5 ffkkkfff 6 ffsdfdfs I would like something like this: 1 aaaaa 2 3 bbbbbb 4 5 cccccc And so... (6 Replies)
Discussion started by: wisher115
6 Replies

8. Shell Programming and Scripting

Numbering

I'm trying to do a script that will look for a log file if it is already there change the name to another name. I.E if log.0 is there rename to log.1 rename log.1 to log.2 rename log.2 to log.3 and so on. Only thing is I got no idea where or what is the best command to use for this? ... (3 Replies)
Discussion started by: merlin
3 Replies

9. UNIX for Dummies Questions & Answers

Numbering!

Just a shot question... how to make 1,2,3,...999 into the form of 001,002,003....999 (3 digits) Thanks.... (9 Replies)
Discussion started by: biglemon
9 Replies

10. UNIX for Dummies Questions & Answers

numbering of process

:confused: How does UNIX handle the numbering of processes? (2 Replies)
Discussion started by: tweety111
2 Replies
Login or Register to Ask a Question