Suppose I have the following variable

$string = 'cat somecommandhere'

Now the output of the above cat command is

test="Thanks serverfault"

I want to extract the value between the quotes and store it in the same variable $string, including the quotes. I want string to contain value below:

$string = "Thanks serverfault"  

How can this be done with sed or grep but not awk. I haven't learned awk yet.

migrated from serverfault.com Mar 4 '13 at 10:09

This question came from our site for system and network administrators.

  • 2
    Why are you resistant to learning what is likely the best tool to accomplish this? – EEAA Mar 2 '13 at 16:16
  • i prefer to learn sed and awk separately so that i don't get confused – Werulz Mar 2 '13 at 16:59
  • Part of learning how to use those two tools is recognizing the best one for the job; it's not like learning a programming language, and it makes things much harder on yourself if you try to use the wrong one. It's also worth noting that cut is a specialized tool with few options to learn, as opposed to awk and sed which have their own associated sub-languages. – Andrew B Mar 2 '13 at 19:26
  • If it can't be done in sed or grep you guys should just say so. And then present a case for awk. – Stefan Lasiewski Mar 4 '13 at 5:47

I'll use cut, since it's fairly easy to deducate that -f2 means "the second part" and -d'"' means cut on the double quotation mark.

$ output='test="Thanks serverfault"'
$ string=$( echo $output | cut -f2 -d'"' )
$ echo $string
Thanks serverfault

If you really want the quotes, you can just add them back yourself, it just makes the answer look more confusing than it needs to be.

If you really want to use sed (lucky for you, this keeps the quotes too):

$ output='test="Thanks serverfault"'
$ string=$( echo $output | sed 's/^test=//' )
$ echo $string
"Thanks serverfault"

Explanation of sed:

  string find/replace mode ^ |     ^ replace with nothing
                             starts with test=
  • Thanks Jay dude i am learning sed btw and was expecting an answer involving sed – Werulz Mar 2 '13 at 17:02
  • dude sorry for not saying this in thread but there might be other text in the file that i will edit via bash ....just give me a sed command that will extract the value between the first " " in file – Werulz Mar 2 '13 at 17:14
  • grep has an option for only returning the matched string. echo $output | grep -o '"[^"]*"' would achieve what you do above with sed. – Ladadadada Mar 2 '13 at 18:31
  • @Ladadadada, thanks, I didn't know that. Werulz, I hope that answers your question. – Jay Mar 2 '13 at 18:37

No need for sed; bash and sh have variable modifiers built in:

$ output='test="Thanks serverfault"'
$ string=\"${output#*\"}
$ string=${string%\"*}\"
$ echo $string
"Thanks serverfault"

# removes prefixes, % is suffixes.

This should be faster than calling external programs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy