I have output from VBoxManage list vms which looks like this:

"arch" {de1a1db2-86c5-43e7-a8de-a0031835f7a7}   
"arch2" {92d8513c-f13e-41b5-97e2-2a6b17d47b67}  

I need to grab the names arch and arch2 and save them into a variable.

7 Answers 7


Using grep + sed

This will parse the contents of those 2 strings:

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

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:


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

Other methods

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

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

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

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.

  • 1
    tr -d \" is another way to delete the quotes. (trnormally translates one set of characters into another; -d tells it to just delete them instead.)
    – deltab
    Jun 14, 2014 at 0:00
  • 1
    slm - if you add an /address/ to sed like sed '/^"\(arch[^"]*\)/s//\1/ you'll only operate on lines containing that string.
    – mikeserv
    Jun 15, 2014 at 0:15
  • 1
    @mikeserv - true, wasn't sure how consistent the arch was going to be in his output. But if it is, then that would work too.
    – slm
    Jun 15, 2014 at 0:18
  • 1
    good point slm. There's no indication that will be consistent. Sorry.
    – mikeserv
    Jun 15, 2014 at 0:20
  • 2
    I just realized though that your sed really should be doing s/^"\([^"]*\)".*/\1/ just in case there are than only two double-quotes on the line.
    – mikeserv
    Jun 15, 2014 at 6:46

That's another job for cut:

VBoxManage list vms | cut -d \" -f2
  • 4
    Very neat! How it works: cut splits each line into fields using the quote mark as delimiter, then outputs field 2: field 1 is the empty string before the first quote, field 2 is the wanted string between the quotes, and field 3 is the rest of the line.
    – deltab
    Jun 14, 2014 at 0:06

With sed you can do:

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


  • 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.

  • Would you break that sed command up for me please? Jun 13, 2014 at 20:53

And the one through grep oneliner with --perl-regexp option,

VBoxManage list vms | grep -oP '(?<=^\")[^"]*'


(?<=^\")[^"]* -> A lookbehind is used here. It matches any character but not of " zero or more times(once it find a double quotes, it stops matching) which are just after double quotes(only the line that starts with double quotes).

Another Ugly hack through sed,

$ sed '/.*\"\(.*\)\".*/ s//\1/g' file

Using bash, I'd write:

while read vm value; do
    case $vm in
        '"arch"') arch=$value ;;
        '"arch2"') arch2=$value ;;
done < <( VBoxManage list vms )
echo $arch
echo $arch2

since regex has greedy and non-greedy modes, if you have multiple targets on the same line, it would not extract as you wish. Line:

"tom" is a cat, and "jerry" is a mouse. 



Command (greedy mode):

grep -oP '".*"' name

Command (non-greedy mode):

grep -oP '".*?"' name
  • Add | tr -d '"' at the end to remove the quotes. Jun 16, 2023 at 22:33
  • echo "\"tom\" is a cat, and \"jerry\" is a mouse." | tee >(cut -d \" -f 2) >(cut -d \" -f 4) >/dev/null Jun 17, 2023 at 0:11

Here, we are using capturing group:

$ grep -oP '"\K.*(?=")' somefile 

If you want to use ripgrep, then the syntax looks like:

$ rg '"(.*)"' somefile -r '$1' 

In addition to Tiina's answer, if you have multiple targets on the same line

In Tiina's context,If you want to use ripgrep then:

echo "\"tom\" is a cat, and \"jerry\" is a mouse." | rg '"(.*?)"' --only-matching --replace '$1'

If you do not have rigprep (In Tiina's context), then:

echo "\"tom\" is a cat, and \"jerry\" is a mouse." | pee 'cut -d \" -f 2' 'cut -d \" -f 4'


echo "\"tom\" is a cat, and \"jerry\" is a mouse." | pee 'cut -d \" -f 2' 'cut -d \" -f 4' | paste -d' ' - -

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .