LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-23-2009, 04:54 PM   #1
OutThere
LQ Newbie
 
Registered: Dec 2008
Posts: 5

Rep: Reputation: Disabled
Remove everything up to the last numbers of a string w/ sed or awk


I know that the expression for the last numbers in a line is "[0-9]*$" if that makes any more sense. Ive got somthing that looks like "hey1234" and I need "1234" to be output. Of course, it could also read "hey123", or "hey there 12345" It could even read "hey123 there1234" and I would still need "1234", so I cant just remove all the numbers. Thanks.
 
Old 04-23-2009, 05:17 PM   #2
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by OutThere View Post
I know that the expression for the last numbers in a line is "[0-9]*$" if that makes any more sense. Ive got somthing that looks like "hey1234" and I need "1234" to be output. Of course, it could also read "hey123", or "hey there 12345" It could even read "hey123 there1234" and I would still need "1234", so I cant just remove all the numbers. Thanks.
Try tr....

Code:
root@host:~<1>$ echo "hey12345" | tr -d '[:alpha:]'
12345
root@host:~<2>$ echo "hey12345 there" | tr -d '[:alpha:]'
12345 
root@host:~<3>$ echo "heythere12345 " | tr -d '[:alpha:]'
12345 
root@host:~<4>$ echo "hey there12345 " | tr -d '[:alpha:]'
 12345
 
Old 04-23-2009, 05:21 PM   #3
arckane
Member
 
Registered: Sep 2005
Location: UK
Distribution: Gentoo/Debian/Ubuntu
Posts: 308

Rep: Reputation: 39
echo "hey12345" | tr -d [:alpha:] [:space:]
12345

Just to be tidy
 
Old 04-23-2009, 05:26 PM   #4
OutThere
LQ Newbie
 
Registered: Dec 2008
Posts: 5

Original Poster
Rep: Reputation: Disabled
yeesh- sorry- I should have mentioned that I also have non-alphanumeric characters. I'm actually going through Apache logs that have the miliseconds that the request took at the end of each line.
 
Old 04-23-2009, 07:01 PM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
Try
Code:
echo "hey123 there1234" | grep -o '[0-9]*$'
1234

string="hey123 there1234"
echo ${string##*[^0-9]}     # bash parameter substitution
1234
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
SED - remove last four characters from string 3saul Linux - Software 12 01-16-2023 10:21 AM
How to remove everything before the first space in Sed or Awk OutThere Linux - General 1 04-05-2009 10:45 PM
grep, sed, awk or tr - searching words in a string hal8000b Programming 2 03-06-2009 08:04 PM
insert zero before single numbers using sed (or awk) chess Programming 10 10-22-2008 08:06 AM
Remove string in sed twantrd Programming 7 09-13-2006 02:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:44 AM.

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