Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract a quoted value from multiple lines using sed

I'm a total n00b when it comes to using sed and can't quite figure this out...

I've got the following data being output by a command line tool:

ruby:
interpreter:  "ruby"
version:      "1.8.6"
date:         "2010-02-04"
platform:     "i386-mingw32"
patchlevel:   "398"
full_version: "ruby 1.8.6 (2010-02-04 patchlevel 398) [i386-mingw32]"

homes:
gem:          ""
ruby:         "C:\ruby\ruby-1.8.6-p398-i386-mingw32"

binaries:
ruby:         "C:\ruby\ruby-1.8.6-p398-i386-mingw32\bin"
irb:          "C:\ruby\ruby-1.8.6-p398-i386-mingw32\bin\irb.bat"
gem:          "C:\ruby\ruby-1.8.6-p398-i386-mingw32\bin\gem.bat"
rake:         ""

environment:
GEM_HOME:     ""
HOME:         "c:/Users/derick"
IRBRC:        ""
RUBYOPT:      ""

file associations:
.rb:
.rbw:

I want to use sed to extract the values from interpreter and version only, and have them returned as "interpreter-version".

The best I could come up with so far is to use grep to return the entire version line:

pik info | grep -e '^version:.*'

but that returns too much info from the version line, and doesn't include the interpreter (obviously).

How can i use sed to extract only the bits of information that i want, so that i get a result of ruby-1.8.6?

... or grep, or awk, or any other command line tool to get the info i want...

FWIW: i'm not using RVM because this is a windows machine using MinGW32... i'm using Pik, which is similar to RVM but made for windows.

like image 664
Derick Bailey Avatar asked Dec 07 '22 00:12

Derick Bailey


1 Answers

A lot of you are working hard to deal with the quotes. Let awk do it:

pik info | awk -F '"' '
    /^interpreter:/ {interp = $2} 
    /^version:/     {ver = $2}
    interp && ver   {print interp "-" ver; exit}
'
like image 166
glenn jackman Avatar answered Dec 11 '22 11:12

glenn jackman