LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-27-2010, 06:59 AM   #1
csegau
LQ Newbie
 
Registered: Oct 2009
Posts: 27

Rep: Reputation: 0
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
 
Old 05-27-2010, 07:15 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
try echoing the file name into sed:
Code:
echo a10b8c1000 | sed <blah>
 
Old 05-27-2010, 07:27 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Here's a way, using only bash. Put the filename into a variable, then use parameter substitution to extract the substring.
Code:
$ filename='a10b8c1000'
$ echo ${filename##*[^0-9]}
1000
 
Old 05-27-2010, 07:31 AM   #4
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Rep: Reputation: 55
Dear David,

Looks Great and it works !!

Code:
$ filename='a10b8c1000'
$ echo ${filename##*[^0-9]}
1000
What is the significance of ## here ? How does this work, Can you please explain, or any link that helps in understanding echo ${filename##*[^0-9]}.

Last edited by vinaytp; 05-27-2010 at 07:33 AM.
 
Old 05-27-2010, 07:39 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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.
 
Old 05-27-2010, 07:54 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by vinaytp View Post
Can you please explain, or any link that helps in understanding echo ${filename##*[^0-9]}.
Try the parameter substitution link in David the H's post.
 
Old 05-28-2010, 05:09 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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'".
 
Old 05-28-2010, 07:18 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by David the H. View Post
unlike syg00, I love parameter substitution. It's flexible and powerful ...
... and it runs a lot faster than running an external command (which may, of course, not be significant).
 
Old 05-28-2010, 07:24 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Couldn't resist:

Code:
sed -r 's#.*([0-9]*)$#\1#'
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to convert address in current process to filename:line number telia Linux - General 5 01-12-2009 04:06 AM
How to extract the interface number from ifconfig -a pooppp Linux - Networking 4 08-04-2008 04:54 AM
HELP!!! how to use grep to extract a number khong1010 Programming 6 04-18-2008 12:01 AM
Best/Right way to extract disk serial number? itnaa Linux - Hardware 4 08-21-2007 11:13 AM
linux shell - extract filename from and song info from text database d003 Programming 1 07-23-2003 04:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration