5
2
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?
Update 1
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
sed
that 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 RE
Not correct! as I found out later. Apologies for causing the confusion.
Update 2
Have updated the sed
RE to make it more specific based on suggestion by @slhck below, but the issue remains as before.
Update 3
Based on the man
page for this version of sed
it appears that this is a BSD-flavoured version.
1what is your desired output? – Endoro – 2013-06-04T13:02:13.690
1@Endoro I can only guess since the regex here is more complicated than it has to be, but I'd say since the OP wants the fourth capture group, they want everything between
v=
and the next&
, so the video ID. – slhck – 2013-06-04T13:07:51.350@Endoro: I've answered both points in the update to the original question above. Thanks for seeking the clarification. – markvgti – 2013-06-05T03:42:32.937
@markvgti -- If you continue to make questions please clarify soever, what OS and other programs you use. – Endoro – 2013-06-05T04:20:55.847