up vote 2 down vote favorite
share [g+] share [fb]

I would like to use sed to pull out the version number from the command:

svnversion --version

Which gives output like:

svnversion, version 1.6.2 (r37639)
   compiled May 10 2009, 12:41:21

Copyright (C) 2000-2009 CollabNet.
Subversion is open source software, see http://subversion.tigris.org/
This product includes software developed by CollabNet (http://www.Collab.Net/).

And after processing with sed, I'd like just:

1.6.2

So far, I have this monstrosity (due to my ignorance):

svnversion --version | sed s/[\wa-zA-Z//\(\):,]*//g | sed 's/[ ]//' | sed 's/[ ]//' | sed 's/[ ][0-9 ./n/-]*//'

I'm sure there is a simple elegant solution that an expert can provide easily.

Thanks.

-William

link|improve this question

feedback

3 Answers

up vote 4 down vote accepted

I know this isn't using sed, but based upon your output this is easier.

svnversion --version | head -1 | awk '{print $3}'

If you have perl available...

svnversion --version | perl -lne 'print $1 if /version (\d+.\d+.\d+)/'
link|improve this answer
Yep: UNIX command lines are all about using the right tool for the job. – Shannon Nelson Dec 8 '09 at 3:37
feedback

Try this:

svnversion --version | sed -n '/version/ s/.*version \([0-9]\+\.[0-9]\+\.[0-9]\+\) .*/\1/p'

It says:

  • -n --- Don't automatically print output.
  • /version/ --- On lines that include the string "version",
  • s/ - substitute for what's
  • .*version and .* --- between [a sequence of zero or more of any character, the string "version", a space] and [a space, a sequence of zero or more of any character],
  • \(...\) --- capturing
  • [0-9]\+ --- a sequence of one or more digits
  • \. --- followed by a literal period
  • then more digits and periods,
  • /\1/ --- what was captured in the first (and only, in this case) set of parentheses
  • p --- and explicitly print it.

Because automatic printing is suppressed and only the line that includes "version" is selected, the other lines are ignored.

link|improve this answer
feedback

One more way to do it w/o sed: svnversion --version | head -1 | cut -d" " -f3

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.