Extract quoted value from multiple lines with sed

I'm just n00b when it comes to using sed and can't figure it out ...

I have the following data that is output using the 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 values ​​only from interpreter and version and return them as an "interpreter-version".

The best I could think of was to use grep to return the entire version string:

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

but this returns too much information from the version line and does not include the interpreter (obviously).

How can I use sed to extract only bits of information that I want to get the result of ruby-1.8.6 ?

... or grep, or awk, or any other command line tool to get the information I want ...

FWIW: I do not use RVM because it is a Windows machine using MinGW32 ... I use Pik, which is similar to RVM, but made for Windows.

+4
source share
7 answers

Would it be useful to use the string "full_version"?

 pik info | awk '/full_version/ {print $2,$3}' | sed 's/\"//;s/\s/\-/' 

I am sure that the sed bit can be concatenated into an awk bit with sub (), but I will leave it to someone more knowledgeable in awk.

+3
source

A lot of you work hard to deal with quotes. Let awk do this:

 pik info | awk -F '"' ' /^interpreter:/ {interp = $2} /^version:/ {ver = $2} interp && ver {print interp "-" ver; exit} ' 
+4
source

Easy enough to do in pure β€œsed” if you know the β€œhold space”:

 sed -n '/^interpreter: *"\(.*\)"/{ s//\1/ h } /^version: *"\(.*\)"/{ s// \1/ H x s/\n// s/.*/interpreter-version: "&"/p }' 

In general, the script does not print anything ( -n ) unless you command it. The first sample found is the translator line; we write the name of the interpreter, then delete the rest of the material and copy the name of the interpreter (plus a new line) into the hold space (' h '). The second pattern found is the version string. We also isolate only the version number - its prefix with a space. Then we combine the version number into the hold space (' h ') and replace the hold and template spaces (' x '). We remove a new line between the name of the interpreter and the version number; then attach it " interpreter-version: " "and follow it with a closing double quote and print the result:

 interpreter-version: "ruby 1.8.6" 

And if you really want "ruby-1.8.6", then the changes are minor:

 sed -n '/^interpreter: *"\(.*\)"/{ s//\1/ h } /^version: *"\(.*\)"/{ s//-\1/ H x s/\n//p }' 

This is the version prefix with a dash instead of a space, and only after that you need to delete a new line before printing after exchanging hold spaces and templates.

+3
source

1) to capture in the file what is displayed using the command line tool, that is, something β†’ outfile 2) in the bash / ubuntu window

 grep -e "interpreter" -e "version" outfile | awk 'BEGIN {print"Interpreter-Version" }{print $2}'| sed 's/"//g'| tr '\n' ' ' 

3) Output:

 Interpreter-Version ruby 1.8.6 

I'm sure it can be optimized, but it worked for me :-)

NTN

Chris.

+2
source

This should do the trick:

 pik info | awk '/^interpreter:/ {inter=$2; gsub(/"/, "", inter)} /^version:/ {vers=$2; gsub(/"/, "", vers)} END {print inter "-" vers}' 

generates ...

 ruby-1.8.6 
+1
source

Version using both awk and sed .

 pik info | awk '/^interpreter:|^version:/ {printf "%s-", $2}' | sed -e 's/"//g' -e 's/-$//' 
+1
source

Using sed only:

 pik info | sed -n '/^full_version: "\([^(]*\)(.*/ {s//\1/; s/ /-/;p}' 
+1
source

All Articles