Alter existing script to work with longer file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Alter existing script to work with longer file name
# 1  
Old 10-23-2014
Alter existing script to work with longer file name

I have this script:
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

It works great with file names that are xxx_xxx_v001.aep, but it won't work on this: xxx_xxx_v001_001.aep.

I've tried and tried but just can't figure out why it won't work.
Any help is much appreciated.
Thanks.
# 2  
Old 10-23-2014
I assume you still want to increment the Vnnn part on the 2nd longer filename.

Also note you can use 10# to avoid the octal issue:

Code:
for file in "$@"
do
        ext=${file##*.}
        base=${file%.*}
        num=${base##*v}
        rest=${num#*_}
        if [ ${#rest} -eq ${#num} ]
        then
            rest=""
        else
            num=${num%%_*}
            rest=_$rest
        fi
        num=$((10#$num+1))
        base=${base%v*}
        new=$(printf '%sv%04d%s.%s' "$base" "$num" "$rest" "$ext")
        echo cp -nv "$file" "$new"
done

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 10-23-2014
That's useful but not exactly what I'm looking for

That works but changes the first number.
I need one that changes the last number and ignores the first.

So, 093_180_v001_004.aep ; would become 093_180_v001_005.aep

The thing is that the first number will not always be v001 ...

Doable?
# 4  
Old 10-23-2014
OK that is actually a little easier:

Code:
for file in "$@"
do
        ext=${file##*.}
        base=${file%.*}
        num=${base##*[v_]}
        base=${base%$num}
        num=$((10#$num+1))
        new=$(printf '%s%03d.%s' "$base" "$num" "$ext")
        cp -nv "$file" "$new"
done

# 5  
Old 10-27-2014
Yes! That works perfectly. Thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Alter awk script to reformat output

Data: 0,mfrh_green_screen,1455432969,37540,/prod/test/system/sys/unikixmain.log,3.0M,mfrh_green_screen,3120660,0,36964--37540 0,mfrh_green_screen,1455433269,38100,/prod/test/system/sys/unikixmain.log,3.1M,mfrh_green_screen,3164223,0,37540--38100... (1 Reply)
Discussion started by: SkySmart
1 Replies

2. Shell Programming and Scripting

Alter Fixed Width File

Thank u so much .Its working fine as expected. ---------- Post updated at 03:41 PM ---------- Previous update was at 01:46 PM ---------- I need one more help. I have another file(fixed length) that will get negative value (ex:-00000000003000) in postion (98 - 112) then i have to... (6 Replies)
Discussion started by: vinus
6 Replies

3. Programming

SQL: Alter existing bool after printing

I'm writing a DB to manage books & dvd's for the library. So after they added more books/dvd's, they press the print button and all newly added entries are printed. That is, as it prints all 'printed = false' entries, which (false) is the default value for that field for each new entry. ... (3 Replies)
Discussion started by: sea
3 Replies

4. UNIX for Dummies Questions & Answers

alter data in a file

Hi , I have data in file like below. status ----------- ------ 2287 C 1502 E 19 can anyone pls help me how can i get it modified as below status 2287|C|1502 E|19 can someone pls help. Thanks. (2 Replies)
Discussion started by: gaddamja
2 Replies

5. Shell Programming and Scripting

shell script to alter grub menu.lst

Hi folks, I have a dual-boot Ubuntu/Windows machine and I wanted to create a script to change the menu.lst file so it will change the default boot partition (this is so I can reload the machine remotely and allow it to boot to the Windows partition). Today I have to sudo cp a template file I... (1 Reply)
Discussion started by: ppucci
1 Replies

6. Shell Programming and Scripting

folder existing and file existing

I want to look into a folder to see if there are any folders within it. If there are, I need to check inside each folder to see if it contains a .pdf file So If /myserver/myfolder/ contains a folder AND that folder conatins a .pdf file do X Else do Z I may have multiple folders and... (4 Replies)
Discussion started by: crowman
4 Replies

7. Shell Programming and Scripting

File Alter Problem--need help

i have 3 files a.txt , b.txt and c.txt each files have keyfields and some column fields e.g. a.txt keyfield1 keyfield2 keyfield3 col1 col2 col3 1 2 3 44 55 66 4 5 6 92 48 33 .....................etc.................. b.txt keyfield1... (2 Replies)
Discussion started by: manas_ranjan
2 Replies

8. Shell Programming and Scripting

writeExcelXML will work on existing excel sheet

I want to know that will SpreadSheet::WriteExcelXML will work on the existing excel file. (1 Reply)
Discussion started by: akash
1 Replies

9. Shell Programming and Scripting

Alter Table Shell Script

I want to add some columns to a existing tables through a shell script. Please help. (2 Replies)
Discussion started by: ankitgupta
2 Replies
Login or Register to Ask a Question