LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 07: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, 08:15 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
try echoing the file name into sed:
Code:
echo a10b8c1000 | sed <blah>
 
Old 05-27-2010, 08:27 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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, 08:31 AM   #4
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

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 08:33 AM.
 
Old 05-27-2010, 08:39 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,843

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
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, 08:54 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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, 06:09 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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, 08:18 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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, 08:24 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Couldn't resist:

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05:06 AM
How to extract the interface number from ifconfig -a pooppp Linux - Networking 4 08-04-2008 05:54 AM
HELP!!! how to use grep to extract a number khong1010 Programming 6 04-18-2008 01:01 AM
Best/Right way to extract disk serial number? itnaa Linux - Hardware 4 08-21-2007 12:13 PM
linux shell - extract filename from and song info from text database d003 Programming 1 07-23-2003 05:06 AM


All times are GMT -5. The time now is 06:23 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration