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
Related videos on Youtube
Author by
Michael Riordan
Updated on September 18, 2022Comments
-
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 over 6 yearsDoesn't make much sense to me to use
grep -P
ifgrep -E
does the same. -
RomanPerekhrest over 6 years@HaukeLaging, true, updated. (Unintentionally missed)
-
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 ofsed
questions on this site, you’ll see that you hardly ever need to pipesed
intosed
. A three-sed
pipeline is definitely unnecessary. -
B. Shea over 4 yearsYou 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.