views:

454

answers:

2

I need to lift the YouTube link from some text which looks like this:

[youtube=http://www.youtube.com/v/qpbAe2HyzqA&hl=en&fs=1&]

Can anyone help?

+2  A: 

Try something like this:

\[youtube=(https?://[^\]]+)\]

Andrew Hare
This did the trick. Thanks, Andrew.
kidrobot
A: 

You could use awk.

awk ' FS="[" {print $(NF) } ' file_with_text > temp.txt
awk ' FS="]" {print $(NF-1)} ' temp.txt > results.txt

It is in two parts to make it clearer and because awk is strange like that. If you want just the URL and not the youtube= first then you will need to run an awk with the file separator like FS="youtube=". Also awk can be strange with the input; if file_with_text has text on the first line it may act strange and if the file ends with the file separator you chose then awk may error (just add any text other than the FS symbol to the end of the file).

Edit: Removed the cat function. Seems less clear as a pedagogical answer, but it is more concise.

Alex
Another unnecessary use of `cat`: instead of `cat FILE | awk PATTERN FILE`, just use `awk PATTERN FILE`.
Seth Johnson