LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 03-20-2010, 05:19 AM   #1
Guyverix
Member
 
Registered: Nov 2008
Location: Washington State
Distribution: Mint
Posts: 36

Rep: Reputation: 2
sed remove string until final match


I have been fighting with a sed statement trying to get it to remove everything in a string until the last match and have been failing badly. I need advice on how to get this to work..

sed --> enterprises.9.9.171.1.5.2.1.1.5
returns 5

I want sed to strip everything out until the last period. The final digit can and will change. Some parts before the final period can change as well, since enterprises will sometimes also be represented as more numbers and periods.

If someone could point me in the direction needed, that would be great!
Thanks,

Chris
 
Old 03-20-2010, 05:23 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Something like this: sed 's/\([0-9a-zA-Z]*\)\..*\(\.[0-9]\)/\1\2/' input

Sample run:
Code:
$ cat input 
enterprises.9.9.171.1.5.2.1.1.5
foo123.9.9.171.1.5.2.1.1.6
456bar.9.9.171.1.5.2.1.1.7

$ sed 's/\([0-9a-zA-Z]*\)\..*\(\.[0-9]\)/\1\2/' input 
enterprises.5
foo123.6
456bar.7

Hope this helps.
 
1 members found this post helpful.
Old 03-20-2010, 05:29 AM   #3
mario.almeida
Member
 
Registered: May 2008
Location: India
Distribution: Ubuntu 10.04, CentOS, Manjaro
Posts: 179

Rep: Reputation: 27
Hi,
try this

x=enterprises.9.9.171.1.5.2.1.1.5; echo ${x##*.}

or

echo $x | awk -F. '{print $NF}'

Last edited by mario.almeida; 03-20-2010 at 05:30 AM.
 
1 members found this post helpful.
Old 03-20-2010, 05:32 AM   #4
Guyverix
Member
 
Registered: Nov 2008
Location: Washington State
Distribution: Mint
Posts: 36

Original Poster
Rep: Reputation: 2
It helps quite a bit.. I still have to figure out that statement for next time. I have needed to do this more than once, and the results have always been ugly.

Is there a way that you could break down the regex logic there? I am having trouble following it..
 
Old 03-20-2010, 05:34 AM   #5
Guyverix
Member
 
Registered: Nov 2008
Location: Washington State
Distribution: Mint
Posts: 36

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by mario.almeida View Post
Hi,
try this

x=enterprises.9.9.171.1.5.2.1.1.5; echo ${x##*.}

or

echo $x | awk -F. '{print $NF}'
Great! That got it perfectly! Thank you!
 
Old 03-20-2010, 05:38 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
I guess I misunderstood the question (you only seem to want the last dot and the last number). If that is true:

sed 's/.*\(\.[0-9]\)/\1/' input

This will look for everything ( the first .* ) and a dot followed by any number ( the .[0-9] part). The \( and \) are backreferences, which can be used in the replace part (the \1 part).

Testrun:
Code:
$ sed 's/.*\(\.[0-9]\)/\1/' input 
.5
.6
.7
Hope this helps.
 
Old 03-20-2010, 06:32 AM   #7
nonamenobody
Member
 
Registered: Oct 2002
Posts: 138

Rep: Reputation: 22
Just to explain druuna's answer for Guyverix. The following searches for anything '.*', followed by a fullstop and a single number '\.[0-9]'. Because the last pattern is in parenthesis it is a sub-expression that can used in the replacement.

Code:
$ sed 's/.*\(\.[0-9]\)/\1/' input
Infact, because you want to return a full stop and the last number unchanged you can just use:
Code:
's/.*\././'
Using this the last number won't be matched or replaced and everything upto it will be replaced by a full stop - the number is not being replaced so doesn't need to be included in the search and replace, however the full stop is needed to in the search and replace, because this is our trigger to stop searching.

However, Guyverix didn't want the full stop so all that is needed is:
Code:
's/.*\.//'
BTW, I particularly like Mario's reply - Bash's shell parameter expansion is a very powerful and under-utilised feature.

Last edited by nonamenobody; 03-20-2010 at 06:35 AM.
 
  


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
SED - remove last four characters from string 3saul Linux - Software 12 01-16-2023 10:21 AM
[SOLVED] Using sed to remove text in fgrep string manwithaplan Linux - General 6 10-11-2009 01:00 AM
Remove everything up to the last numbers of a string w/ sed or awk OutThere Linux - General 4 04-23-2009 07:01 PM
Trying to use sed to remove last line if it contains a certain string. slaxative Linux - Software 1 03-18-2008 02:13 AM
Remove string in sed twantrd Programming 7 09-13-2006 02:28 PM

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

All times are GMT -5. The time now is 10:11 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