Extract everything between quotes

5,324

Solution 1

Using sed:

sed -E 's/.*\("(.*)"\).*/\1/'

Example:

echo 'javascript:open_window("http://www.example.com/somescript.ext?withquerystring=true")' | sed -E 's/.*\("(.*)"\).*/\1/'
http://www.example.com/somescript.ext?withquerystring=true

Solution 2

Simply with GNU grep:

s='javascript:open_window("http://www.example.com/somescript.ext?withquerystring=true");'
grep -Eo 'http:[^"]+' <<<"$s"
http://www.example.com/somescript.ext?withquerystring=true

Solution 3

awk 'BEGIN {FS = "\42"} {print $2}' <<'eof'
javascript:open_window("http://www.example.com/somescript.ext?withquerystring=true");
eof
Share:
5,324

Related videos on Youtube

Michael Riordan
Author by

Michael Riordan

Updated on September 18, 2022

Comments

  • Michael Riordan
    Michael Riordan over 1 year

    I am trying to use grep or sed to extract a url from a string which looks like javascript:open_window("http://www.example.com/somescript.ext?withquerystring=true");

    The javascript link is generated -- by an external application I have no control over -- each time, so I have to extract the URL to use it. I have tried and failed to use a whole host of combinations of grep and sed, which haven't worked.

  • Hauke Laging
    Hauke Laging over 6 years
    Doesn't make much sense to me to use grep -P if grep -E does the same.
  • RomanPerekhrest
    RomanPerekhrest over 6 years
    @HaukeLaging, true, updated. (Unintentionally missed)
  • Scott - Слава Україні
    Scott - Слава Україні over 6 years
    (1) Why bother?  There are two sed answers ahead of yours, and they are both simpler than yours. (2) If you read the documentation, or any of the thousands of sed questions on this site, you’ll see that you hardly ever need to pipe sed into sed.  A three-sed pipeline is definitely unnecessary.
  • B. Shea
    B. Shea over 4 years
    You are looking for parenthesis? Why? Just remove those: sed -E 's/.*"(.*)".*/\1/' Question is: "Extract everything between quotes" not "Extract everything between quotes with parenthesis". Make your answer more generalized - it will still work for OP.