extract last number from filename
Hi all,
I have to extract last number from filename. ex- my file name is a10b8c1000 so i want to extract 1000 from it. i tried using sed sed 's/[a-z][0-9]*[a-z][0-9]*[a-z]//g' a10b8c1000 but sed looks for content inside file. Please help me... Thanks |
try echoing the file name into sed:
Code:
echo a10b8c1000 | sed <blah> |
Here's a way, using only bash. Put the filename into a variable, then use parameter substitution to extract the substring.
Code:
$ filename='a10b8c1000' |
Dear David,
Looks Great and it works !! Code:
$ filename='a10b8c1000' |
Every so often I remember parameter substitution - but I always have to go look it up. It just ain't natural.
regex just "is" - egrep in this case for me. |
Quote:
|
unlike syg00, I love parameter substitution. It's flexible and powerful and I don't find it difficult at all. You really only need to memorize a handful of patterns to get most of the benefit, and many of those are similar to ones you encounter elsewhere. It's also more efficient than calling on external tools like sed. Of course it can't tackle everything, but it's great for doing simple text manipulations.
And yes, I posted the link so you could learn more about it yourself. But to summarize the pattern I used, it says "remove from the beginning the longest string that matches 'anything + not a number'". |
Quote:
|
Couldn't resist:
Code:
sed -r 's#.*([0-9]*)$#\1#' |
All times are GMT -5. The time now is 01:13 PM. |