Visit Our UNIX and Linux User Community


change the filename by adding up 1 each time, tricky one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting change the filename by adding up 1 each time, tricky one
# 1  
Old 02-29-2008
Question change the filename by adding up 1 each time, tricky one

Smilie


Hi, I posted here before for adding up of datafile name each time, here is an example:

#!/bin/bash

cutdfname="data11.dbf"

newname=$(echo "${cutdfname}" |tr "[A-Z]" "[a-z]" |tr "[a-z]#_@-" "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |tr -s "x")
num=$(echo $newname |cut -d"." -f1|awk -F"x" '{print $NF}')
if [ -z "${num}" ] ; then
num=1
newnum=$((num+1))
finaldfname=$(echo $cutdfname|sed -e "s/\./$newnum\./g")
else
newnum=$((num+1))
finaldfname=$(echo $cutdfname|sed -e "s/$num\./$newnum\./g")
fi
echo "$cutdfname -> $finaldfname"
fulldfname=$lastdf/$finaldfname
echo "fulldfname is $fulldfname"


./cal_file_name.bsh
data11.dbf -> data12.dbf
fulldfname is /data12.dbf


but it failed only if datafile name like: 09 Smilie

for example:

#!/bin/bash

cutdfname="data09.dbf"

newname=$(echo "${cutdfname}" |tr "[A-Z]" "[a-z]" |tr "[a-z]#_@-" "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |tr -s "x")
num=$(echo $newname |cut -d"." -f1|awk -F"x" '{print $NF}')
if [ -z "${num}" ] ; then
num=1
newnum=$((num+1))
finaldfname=$(echo $cutdfname|sed -e "s/\./$newnum\./g")
else
newnum=$((num+1))
finaldfname=$(echo $cutdfname|sed -e "s/$num\./$newnum\./g")
fi
echo "$cutdfname -> $finaldfname"
fulldfname=$lastdf/$finaldfname
echo "fulldfname is $fulldfname"

./cal_file_name.bsh
./cal_file_name.bsh: 09: value too great for base (error token is "09")
data09.dbf ->
fulldfname is /



can someone tune it to be perfect?

Thank you and have a nice weekend!
# 2  
Old 03-01-2008
How about...
Code:
$ cat newname
#! /usr/local/bin/bash

shopt -s extglob

for name ; do
        suffix=${name#*.}
        main=${name%.$suffix}
        prefix=${main%%+([0-9])}
        num=${main#$prefix}
        num=10#${num}
        newnum=$((num+1))
        newname=${prefix}${newnum}.${suffix}
        echo $name "->" $newname
done
exit 0
$
$
$ ./newname abc111.dbf 0.dbf 09.dbf qwerty09.dbf
abc111.dbf -> abc112.dbf
0.dbf -> 1.dbf
09.dbf -> 10.dbf
qwerty09.dbf -> qwerty10.dbf
$

# 3  
Old 03-03-2008
Perderado:
Your code does not work for me.
First it not gets prefix by:
prefix=${main%%+([0-9])}
result is the $main
After that it does not take a number.

Can you give some explanation how you works with ${name}
It is not understandable how you have name broken on parts by that sintaxis.

Thank you
# 4  
Old 03-03-2008
Note that posted the results of the test I performed with my code. It works for me. Did you leave out that shopt statement? That would make it fail.
# 5  
Old 03-04-2008
Perderabo - I beleive you that your code does work, therefore I have asked about some comments: what those characters mean and how that happened?
I could not fine any reasonable explanation. I see you using regular expresion filtering and setting extglob additionaly the extended reg-expr, but I could not understand meaning of useg characters.
Also it is surprizing to use a reg-expr inside of a variable name. That also is not obviose how it is works.

YOu are right, after setting the extglob option it is working fine.

Would you, please, give some explanation on parsing the name by different exprecion?!

Thank you!
# 6  
Old 03-07-2008
I have undestood the used syntax (with help from another person)
Quote:
Originally Posted by cfajohnson
You don't use regular expressions in parameter expansion; you use
filename expansion (a.k.a. wildcard) patterns.
and replay to my question by myself.
It is parameter expansion POSIX shell parameter expansions

And that means:
Code:
suffix=${name#*.}

- # remove shortest part from beginning for matching *. - so for name="abc.def.gh" it removes the "abc." and returtns "def.gh", although "abc.def." is also matching the *.
So, this statement means - everything after first dot.

Code:
main=${name%.$suffix}

- % remove shortest part from end for matching .$suffix - So, it is removing suffix with dot from end, and returns beginning before .

Shorter it could be done with the same result by:
Code:
main=${name##*.}

- but the suffix is used in final construction
The double % and # ( %% and ## ) means to remove longer matching, instead of shorter

Now the
Code:
prefix=${main%%+([0-9])}

means : get part before longer line of consecutive digits on end of the 'main'
: '+' - one or more repetition; [0-9] - any digits

and
Code:
num=${main#$prefix}

- obviose is that just removed in previose statement string of digits

The statement num=10#${num} adds the base of the number. Othervise the '09' would be treated as 8-base number and rase an error (9 - is out of 0-7 digits)
Acctualy, last two statement before final file name construction could be done in that construction:
Code:
newname=${prefix}$((10#$num+1)).${suffix}

The '$((' and '))' define arithmetic calculation, '10#' before variable annonce the base for number.

Last edited by alex_5161; 11-05-2008 at 09:03 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #110
Difficulty: Easy
As of 2014, Apple's macOS was the largest Unix installed base globally.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding timestap to filename using perl

Hello, I am trying to create a file in windows and i want the filename to have timestamp as well but something is wrong and i can not understand waht. The code that i use is the following ($cwkday,$cmonth,$cday,$ctime,$cyear) = split(/\s+/, localtime); $current_date =... (5 Replies)
Discussion started by: chriss_58
5 Replies

2. Shell Programming and Scripting

Adding time to date time in UNIX shell scipting

I needed some help in adding a duration (in seconds) to a start time (in hhmmss format) and a start date (in mmddyy format) in order to get an end date and end time. The concept of a leap year is also to be considered while incrementing the day. The code/ function that I have formed so far is as... (3 Replies)
Discussion started by: codehelp04
3 Replies

3. UNIX for Dummies Questions & Answers

Adding Filename as column using sed

Hi , Can any one please tell me, how can we add the file name as column using sed. right now we are using the below awk command for adding the file name as column but when we are calling this script from datastage it is deleting the file data..very weird raised a support ticket with datastage.... (2 Replies)
Discussion started by: mora
2 Replies

4. Shell Programming and Scripting

Adding filename to each line of the file

Hi, I am a relative new bee in scripting. I need to develop a script such that the code would iterate through each file in a source directory and append every line of the file with '|' and the corresponding file filename. eg INPUT file IF927_1.dat - H|abc... (4 Replies)
Discussion started by: scripting_newbe
4 Replies

5. Shell Programming and Scripting

Help with adding leading zeros to a filename

Hi i need help in adding leading zero to filenames e.g file name in my folder are 1_234sd.txt 23_234sd.txt the output i need is 001_234sd.txt 023_234sd.txt can i do this shell scripting please help (2 Replies)
Discussion started by: rsmpk
2 Replies

6. UNIX for Dummies Questions & Answers

Adding Date & time stamps to filename

I need to edit the file name with date and time while writing the script. please help. (1 Reply)
Discussion started by: manish.s
1 Replies

7. Shell Programming and Scripting

gzcat into awk and then change FILENAME and process new FILENAME

I am trying to write a script that prompts users for date and time, then process the gzip file into awk. During the ksh part of the script another file is created and needs to be processed with a different set of pattern matches then I need to combine the two in the end. I'm stuck at the part... (6 Replies)
Discussion started by: timj123
6 Replies

8. Shell Programming and Scripting

a bit tricky to change it multiple rows in one row and ...

Hi, I have an output file like: 1415971694 376 (12); 3434327831 376 (7); 2989082873 332 (3); 4075357577 332 (3); 1221064374 376 (2); 2372410470 376 (2); 2563564778 332 (2); 443221432 376 (1); ... (2 Replies)
Discussion started by: netbanker
2 Replies

9. Shell Programming and Scripting

Adding filename into file content

Dear Experts, Please help to teach me how to add the filename into the file content. Actually the file name are EVENTS-20050912. ***************New output that I want*************** EVENTS-20050912 03:33:37 ALARM: BTSSPAN-277-1 30-18013 EVENTS-20050912 12:10:28 ALARM: BTSSPAN-297-2... (1 Reply)
Discussion started by: missutoomuch
1 Replies

10. Shell Programming and Scripting

How to adding the filename into file contents

Dear Experts, Please help to teach me how to add the filename into the file content so that i can get the output below:- Actually the file name ***************New output that I want*************** =====2005-11-12===== EVENTS-20050912 03:33:37 ALARM: BTSSPAN-277-1 30-18013... (2 Replies)
Discussion started by: missutoomuch
2 Replies

Featured Tech Videos