Results 1 to 10 of 16
Hi folks
Please forgive how lame this question must seem to you pros, but I'm a bit stuck here...
On my machine, calling this in a shell:
Code:
g++ --version
...
- 07-13-2010 #1Just Joined!
- Join Date
- Jul 2010
- Posts
- 6
noob sed question
Hi folks
Please forgive how lame this question must seem to you pros, but I'm a bit stuck here...
On my machine, calling this in a shell:
outputs the following:Code:g++ --version
Please can someone help with the sed and/or other shell commands which will be able to parse out the g++ version number. ie, I'm looking for the output from sed to be the following:Code:g++ (SUSE Linux) 4.4.1 [gcc-4_4-branch revision 150839] Copyright (C) 2009 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
At the moment I'm using this command:4.4.1
and getting the following:Code:g++ --version | sed -e 's/[a-Z+()$/[,;]*//g'
which I guess is kinda half-way there... I'm sure there's an easy to way to strip out the leading whitespace, and then keep only the items up until the first whitespace character found... but at this point my limited knowledge of sed fails miserably!Code:4.4.1 -4_4- 150839] 2009 . . .
TIA
Steve
- 07-13-2010 #2
Different approach, but maybe ok-ish for you?
You could query the rpm DB
With gcc being the name of the (installed) rpm.Code:rpm -q --queryformat '%{VERSION}\n' gcc
gcc is gcc´s name on CentOS, havent seen a SuSE in a while, so ymmv
You must always face the curtain with a bow.
- 07-13-2010 #3Just Joined!
- Join Date
- Jul 2010
- Posts
- 6
Thanks Irithori, that may just work. Gives back 4.4 instead of 4.4.1, but I think that won't be an issue.
I'd still like to know the sed command though, coz no doubt there will be a million more uses for knowing that in the future?!
- 07-13-2010 #4Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
- 07-14-2010 #5Just Joined!
- Join Date
- Feb 2009
- Location
- Southport, England
- Posts
- 31
- 07-14-2010 #6Just Joined!
- Join Date
- Jul 2010
- Posts
- 6
Thanks Franklin52 - I'd say that the sed one works best for me - results from all 3 possible solutions below.
The problem with the awk one is that I get back the 4th word in each line, of which there are 3. Is there a way to get awk to return only the 1st line?
lemons, the fact that you're getting back major.minor.revision whereas I'm only getting major.minor means that using the rpm query is not portableCode:slorimer@slorimer:~> g++ --version | awk '/g++/{print $4}' 4.4.1 Free software;
So this is the one that works best. Franklin52, please can you talk me through each of the tokens in the sed command? I mean, come on! It looks like some alien language!Code:slorimer@slorimer:~> rpm -q --queryformat '%{VERSION}\n' gcc 4.4
On another forum I was given this command, which is quite elegant:Code:slorimer@slorimer:~> g++ --version | sed -n '/g++/ s/.*) \(.*\) \[.*/\1/p' 4.4.1
Thanks for all the help folks, it's much appreciated!Code:slorimer@slorimer:~> g++ --version | grep '[0-9]\.[0-9]\.[0-9]' -o 4.4.1
Steve
- 07-14-2010 #7Just Joined!
- Join Date
- May 2010
- Posts
- 5
If awk is ok for you.. Then you can try this
Code:$ gcc --version | grep 4 | awk '{print $3}' 4.2.4
- 07-14-2010 #8Just Joined!
- Join Date
- Jul 2010
- Posts
- 6
That assumes there will always be a 4 in gcc version - which is most definitely not true.
- 07-14-2010 #9Just Joined!
- Join Date
- Apr 2003
- Location
- Netherlands
- Posts
- 20
Given this output for `g++ --version`:
Code:g++ (SUSE Linux) 4.4.1 [gcc-4_4-branch revision 150839] Copyright (C) 2009 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- the -n flag tells sed not to print anything unmatchedCode:g++ --version | sed -n '/g++/ s/.*) \(.*\) \[.*/\1/p'
- line(s) containing 'g++' are matched
The harder part is s/.*) \(.*\) \[.*/\1/p'
- '.' in a sed expression means any character (exactly one)
- '*' means, repeating the previous pattern, zero or more times
- '.*' means, a series of characters of arbitrary length (not otherwise specified)
- \( ... \) is a subexpression - it is referenced by \1 (you can have up to 9 of these, referenced by \1 ... \9 )
- '[' is a special character (for ranges, such as [1-9]) - to have it treated as the actual character, it has to be "escaped", written as "\["
From left to write:
- it matches anything before a ')' in .*)
- then a space
- then a series of characters (of arbitrary length) as the subexpression (intended to be the actual version string!!)
- [ and the rest of the line is matched
then the subexpression is printed
- 07-14-2010 #10Just Joined!
- Join Date
- Apr 2003
- Location
- Netherlands
- Posts
- 20
By the way the sed line doesn't work in general; my `g++ --version` says,
Nothing is matched, as the [ isn't in this outputCode:g++ (Debian 4.3.2-1.1) 4.3.2 Copyright (C) 2008 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I'd adapt the sed line to:
Hoping how that works shouldn't be too hard to decrypt now!Code:g++ --version | sed -n '/g++/ s/.*) \([0-9]\.[0-9]\.[0-9]\).*/\1/p'


Reply With Quote
