How do I extract the content of quoted strings from the output of a command?

Using grep + sed

This will parse the contents of those 2 strings:

$ grep -o '".*"' somefile | sed 's/"//g'
arch
arch2

The above looks for a string matching the pattern ".*". That will match anything that occurs within double quotes. So grep will return these types of values:

"arch"
"arch2"

The pipe to sed will strip off any double quotes from these strings giving your the strings you're looking for. The notation sed 's/"//g' is instructing sed to do a search and replace on all occurrences of double quotes, substituting them with nothing, s/"//g. The command s/find/replace/g is what's going on there, and the trailing g to search tells it to do it globally on the entire string that it's given.

Using just sed

You can also use sed to chop off the beginning double quote, keep what's in between them, and chop off the remaining quote + everything there after:

$ sed 's/^"\(.*\)".*/\1/' a
arch
arch2

Other methods

$ grep -o '".*"' somefile | tr -d '"'
arch
arch2

The command tr can be used to delete characters. In this case it's deleting the double quotes.

$ grep -oP '(?<=").*(?=")' somefile
arch
arch2

Using grep's PCRE feature you can look for any substrings that begin with a double quote or end with a double quote and report just the substring.


That's another job for cut:

VBoxManage list vms | cut -d \" -f2

With sed you can do:

var=$(VBoxManage list vms | sed 's/^"\([^"]*\).*/\1/')

Explanation:

  • s/.../.../ - match and replace
  • ^- match at start of line
  • \(...\) - this is a back reference, we can refer to what is matched in here later with \1
  • [^"]* - match any sequence that does not contain a " (ie up to the the next ")
  • .* - match the rest of the line
  • \1 - replace with the back reference

Or with awk:

var=$(VBoxManage list vms | awk -F\" '{ print $2 }')

Note that in modern shells you can also use an array instead of a normal variable. In bash you can do:

IFS=$'\n'; set -f
array=( $(VBoxManage list vms | awk -F\" '{ print $2 }') )
echo "array[0] = ${array[0]}"
echo "array[1] = ${array[1]}"

This might be easier when you come to use the variable.