Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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 ...
  1. #1
    Just 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:

    Code:
    g++ --version
    outputs 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.
    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:

    4.4.1
    At the moment I'm using this command:

    Code:
    g++ --version | sed -e 's/[a-Z+()$/[,;]*//g'
    and getting the following:

    Code:
       4.4.1 -4_4-  150839]
      2009    .
             .
              .
    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!

    TIA
    Steve

  2. #2
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,096
    Different approach, but maybe ok-ish for you?

    You could query the rpm DB
    Code:
    rpm -q --queryformat '%{VERSION}\n' gcc
    With gcc being the name of the (installed) rpm.
    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.

  3. #3
    Just 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?!

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by stevelorimer View Post
    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?!
    Try this:

    Code:
    g++ --version | sed -n '/g++/ s/.*) \(.*\) \[.*/\1/p'
    or shorter with awk:

    Code:
    g++ --version | awk '/g++/{print $4}'

  5. #5
    Just Joined!
    Join Date
    Feb 2009
    Location
    Southport, England
    Posts
    31
    Quote Originally Posted by stevelorimer View Post
    Thanks Irithori, that may just work. Gives back 4.4 instead of 4.4.1, but I think that won't be an issue...
    For me, the command mentioned by Irithori seems to return:

    Code:
    [adam@computer ~]$ rpm -q --queryformat '%{VERSION}\n' gcc
    4.4.4
    (on Fedora 12.)

  6. #6
    Just 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?

    Code:
    slorimer@slorimer:~> g++ --version | awk '/g++/{print $4}'
    4.4.1
    Free
    software;
    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 portable

    Code:
    slorimer@slorimer:~> rpm -q --queryformat '%{VERSION}\n' gcc
    4.4
    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:~> g++ --version | sed -n '/g++/ s/.*) \(.*\) \[.*/\1/p'
    4.4.1
    On another forum I was given this command, which is quite elegant:

    Code:
    slorimer@slorimer:~> g++ --version | grep '[0-9]\.[0-9]\.[0-9]' -o
    4.4.1
    Thanks for all the help folks, it's much appreciated!
    Steve

  7. #7
    Just 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

  8. #8
    Just Joined!
    Join Date
    Jul 2010
    Posts
    6
    That assumes there will always be a 4 in gcc version - which is most definitely not true.

  9. #9
    Just 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.
    Code:
    g++ --version | sed -n '/g++/ s/.*) \(.*\) \[.*/\1/p'
    - the -n flag tells sed not to print anything unmatched
    - 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

  10. #10
    Just Joined!
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    20
    By the way the sed line doesn't work in general; my `g++ --version` says,
    Code:
    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.
    Nothing is matched, as the [ isn't in this output

    I'd adapt the sed line to:
    Code:
    g++ --version |   sed -n '/g++/ s/.*) \([0-9]\.[0-9]\.[0-9]\).*/\1/p'
    Hoping how that works shouldn't be too hard to decrypt now!

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...