The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
a bit tricky to change it multiple rows in one row and ... netbanker Shell Programming and Scripting 2 01-01-2008 01:48 AM
Grabing Date from filename and adding to the end of each line in the file. rkumar28 Shell Programming and Scripting 1 05-01-2007 08:25 PM
Change new filename with date ?? sabercats Shell Programming and Scripting 9 02-13-2006 05:12 PM
Adding filename into file content missutoomuch Shell Programming and Scripting 1 11-11-2005 04:02 PM
How to adding the filename into file contents missutoomuch Shell Programming and Scripting 2 11-11-2005 05:26 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-29-2008
netbanker netbanker is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 8
Question change the filename by adding up 1 each time, tricky one




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

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 (permalink)  
Old 03-01-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
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 (permalink)  
Old 03-03-2008
alex_5161 alex_5161 is offline
Registered User
  
 

Join Date: Jan 2007
Location: Detroit
Posts: 124
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 (permalink)  
Old 03-03-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
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 (permalink)  
Old 03-04-2008
alex_5161 alex_5161 is offline
Registered User
  
 

Join Date: Jan 2007
Location: Detroit
Posts: 124
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 (permalink)  
Old 03-07-2008
alex_5161 alex_5161 is offline
Registered User
  
 

Join Date: Jan 2007
Location: Detroit
Posts: 124
I have undestood the used syntax (with help from another person)
Quote:
Originally Posted by cfajohnson View Post
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 08:03 PM..
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:36 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0