I am trying to use sed to extract the value part of one of the many key-value pairs in a URL's query string
This is what I am trying:
echo 'http://www.youtube.com/watch?v=abc&g=xyz' | sed 's@^https?://(www.)?youtube.com/(watch\\?)?.*?v(=|/)([a-zA-Z0-9\-_]*)(&.*)?$@$4@'
but it always outputs the input URL as is.
What am I doing wrong?
To clarify some issues:
- The regex is more complicated than it has to be because I am also trying to check the validity of the input and generate the output only if the input is valid. So a stricter match.
- The desired output is the value of the key 'v' in the query string.
- Have been unable to find the version of
sedthat I am using, but it's the one that comes with Mac OS X (10.7.5).
- In my version of
sed$1, $2 etc. seem to be the matches, \1, \2 etc. give the error:
sed: 1: "s@^https?://(www.)?yout ...": \4 not defined in the RENot correct! as I found out later. Apologies for causing the confusion.
Have updated the
sed RE to make it more specific based on suggestion by @slhck below, but the issue remains as before.
Based on the
man page for this version of
sed it appears that this is a BSD-flavoured version.