You are not logged in.
Pages: 1
Hi,
I want to cut a string in bash to remove a number in the string, and save that number for further formatting.
At the moment, I am doing it with two double sed's, but I was wondering if it is possible to do it more elegantly.
Here's an example of what I want to do.
Given the String=Agora1.gif
I want to get the the number 1 out of the string.
So, what I am doing is
Number=`echo $String | sed "s/Agora//" | sed "s/\.gif//"`
That will save the number. I can later change the name;
NewName=`printf "Agora%02i" $Number`
and use the new name to rename the original file
mv $String $NewName
I want to do this for a bath of photographs. So far, it works, but I feel that it could be improved.
Offline
man rename has an example about this exact same problem.
As for doing it in sed, you should look into using \(, \) and \1, edit: and [0-9]*
Last edited by Procyon (2010-08-05 17:28:46)
Offline
Thanks for the rename command, I didnt know it exists
I'll have a look into \(, \).
Offline
Also note that
NewName=`printf "Agora%02i" $Number`
can be written as:
printf -v NewName "Agora%02i" $Number
This will assign the output of printf to the variable NewName.
Offline
Pages: 1