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 06-26-2015, 03:23 PM   #1
immolation
LQ Newbie
 
Registered: Jun 2013
Posts: 9

Rep: Reputation: Disabled
Sed everything after a number


Hey all! I am new to sed and having some trouble displaying only the version number of a package.

Example:

# rpm -qa shadow-utils
shadow-utils-4.0.17-23.el5

Desired result:
4.0.17-23.el5

I've tried things like:
rpm -qa shadow-utils | sed -n -e 's/^.*\(utils-\)/\1/p'
rpm -qa shadow-utils | sed '1,/utils/d'

Ideally, it could be used for all packages to only display everything after the number. Let me know where I'm going wrong, and thanks!
 
Old 06-26-2015, 05:46 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

this doesn't answer your question, but I think it solves your problem:
Code:
rpm -q --queryformat '%{VERSION}-%{RELEASE}.%{ARCH}\n' shadow-utils
Not sure if you want the arch included... modify as desired.

Evo2.
 
1 members found this post helpful.
Old 06-26-2015, 05:58 PM   #3
immolation
LQ Newbie
 
Registered: Jun 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Evo - thanks for the reply! Your solution is perfect for what I'm trying to do, and I just saw queryformat mentioned in another post and was going to check it out.

Code:
rpm -q --queryformat '%{VERSION}-%{RELEASE}\n' shadow-utils
^^Ended up giving me exactly what I needed. I'll just need to read up on sed more before I need it again. Thanks again!

Quote:
Originally Posted by evo2 View Post
Hi,

this doesn't answer your question, but I think it solves your problem:
Code:
rpm -q --queryformat '%{VERSION}-%{RELEASE}.%{ARCH}\n' shadow-utils
Not sure if you want the arch included... modify as desired.

Evo2.
 
Old 06-27-2015, 04:50 AM   #4
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,692

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Quote:
Originally Posted by immolation View Post
Hey all! I am new to sed and having some trouble displaying only the version number of a package.

Example:

# rpm -qa shadow-utils
shadow-utils-4.0.17-23.el5

Desired result:
4.0.17-23.el5

I've tried things like:
rpm -qa shadow-utils | sed -n -e 's/^.*\(utils-\)/\1/p'
rpm -qa shadow-utils | sed '1,/utils/d'

Ideally, it could be used for all packages to only display everything after the number. Let me know where I'm going wrong, and thanks!
The first sed command will replace everything up to the string "utils-" with this string. In other words, it will remove everything that comes before "utils-". It's overly complex; you need the parentheses only around non-constant regular expressions, and the anchor at the beginning is redundant.
The second command deletes all lines between the beginning and the line containing "utils".
Neither do what you want.

While the queryformat solution is the best, this would also work (can't test it right now):

Code:
sed 's/^[^0-9]*//'
This removes any string at the beginning of the line that doesn't contain a number.
 
2 members found this post helpful.
Old 06-27-2015, 05:10 AM   #5
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 6.7, 7
Posts: 689

Rep: Reputation: 69
Quote:
Originally Posted by berndbausch View Post
The first sed command will replace everything up to the string "utils-" with this string. In other words, it will remove everything that comes before "utils-". It's overly complex; you need the parentheses only around non-constant regular expressions, and the anchor at the beginning is redundant.
The second command deletes all lines between the beginning and the line containing "utils".
Neither do what you want.

While the queryformat solution is the best, this would also work (can't test it right now):

Code:
sed 's/^[^0-9]*//'
This removes any string at the beginning of the line that doesn't contain a number.

Why is the first caret necessary after s/ ?
For me it works even without the first caret. Of course, that doesn't mean anything, with other strings I suppose it's relevant.
So what does your sed command to exactly? Deletes everything up to the string the starts with a digit? If so, why does it also work without the first caret? Is it somehow implied in this context, because the 'g' option hasn't been used and so it stops at the first occurence of a digit?

Indeed, when I try (this my string: shadow-utils-4.1.5.1-18.el7.x86_64) rpm -qa shadow-utils | sed 's/[^0-9]*//g', this is the result:
Quote:
41511878664
So any other character than the digits are supressed.

I guess I've tried to offer an explanation for myself.

But what if I also wanted to supress the last part of the string, beginning from el17.x86_64 or from .x86_64?
How can that be achieved?

Last edited by vincix; 06-27-2015 at 05:12 AM.
 
Old 06-27-2015, 12:42 PM   #6
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,692

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Quote:
Originally Posted by vincix View Post
Why is the first caret necessary after s/ ?
For me it works even without the first caret. Of course, that doesn't mean anything, with other strings I suppose it's relevant.
So what does your sed command to exactly? Deletes everything up to the string the starts with a digit? If so, why does it also work without the first caret? Is it somehow implied in this context, because the 'g' option hasn't been used and so it stops at the first occurence of a digit?
Thanks to the caret, only non-numeric strings at the beginning of the line are replaced.

Quote:
So any other character than the digits are supressed.
Your regular expression means "any non-numeric string", beginning of line or not. The "g" at the end replaces all such strings, not just the first one.

Quote:
But what if I also wanted to supress the last part of the string, beginning from el17.x86_64 or from .x86_64?
How can that be achieved?
If the part you want to delete always starts with el17, do

Code:
s/el17.*//
 
Old 07-01-2015, 03:17 PM   #7
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 6.7, 7
Posts: 689

Rep: Reputation: 69
Quote:
Originally Posted by berndbausch View Post
Thanks to the caret, only non-numeric strings at the beginning of the line are replaced.


Your regular expression means "any non-numeric string", beginning of line or not. The "g" at the end replaces all such strings, not just the first one.



If the part you want to delete always starts with el17, do

Code:
s/el17.*//
Yes, but what I was asking was, how do I get rid of the first part too, up to the version?
 
Old 07-01-2015, 04:37 PM   #8
immolation
LQ Newbie
 
Registered: Jun 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by vincix View Post
Yes, but what I was asking was, how do I get rid of the first part too, up to the version?
I was wondering the same thing; made a note to look more into it (using a single command) but for the time being I got it to work by simply combining the two commands.

Code:
# rpm -q shadow-utils | sed -e 's/^[^0-9]*//' -e 's/.el5.*//'
4.0.17-23
Alternatively:

Code:
# rpm -q shadow-utils | sed 's/^[^0-9]*//;s/.el5.*//'
4.0.17-23

Last edited by immolation; 07-02-2015 at 07:45 PM.
 
1 members found this post helpful.
Old 07-01-2015, 10:57 PM   #9
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,692

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Quote:
Originally Posted by vincix View Post
Yes, but what I was asking was, how do I get rid of the first part too, up to the version?
I don't think I understand which part of which string you want to remove.
 
Old 07-03-2015, 07:28 PM   #10
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 6.7, 7
Posts: 689

Rep: Reputation: 69
Quote:
Originally Posted by berndbausch View Post
I don't think I understand which part of which string you want to remove.
I was referring exactly to what immolation said in his last post.
 
  


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
[SOLVED] Changing pattern using sed [number] to number. xpto09 Linux - General 2 06-22-2011 04:32 PM
[SOLVED] sed, extract a number in a hash conconga Linux - General 3 04-30-2011 11:26 AM
SED ? get number before specific word czezz Programming 3 08-11-2009 07:00 PM
Specifying line number with sed teiben Linux - Newbie 8 06-27-2008 06:54 AM
Pass a variable as a line number in sed dx0r515t Programming 7 03-30-2007 05:55 PM

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

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