1

I am wondering if there is a way I can download (with wget probably) an html file lets say http://vodlocker.com/embed-wrdlm4dbigu4-850x450.html

Here is the html file in pastebin. Here is the important stuff though.

<script type='text/javascript'>  jwplayer("flvplayer").setup({ 
file: "http://77.81.98.228:8777/n2ceexa2lo4pcnokaldsf4o64qg7le7rp2xxamcxtdebx4vta3lae5iz6e/v.mp4",
skin: "beelden",
image: "http://77.81.98.228:8777/i/03/00494/wrdlm4dbigu4.jpg",

I need to extract http://77.81.98.228:8777/n2ceexa2lo4pcnokaldsf4o64qg7le7rp2xxamcxtdebx4vta3lae5iz6e/v.mp4 from that html file.

so the text between file: and ",

I am new to ubuntu and a terminal interface so I am unfamiliar with basic command line codes.

2 Answers 2

0

First, an obligatory warning. It is usually a very bad idea to try to parse HTML with simple tools like regular expressions. That said, in this case, if you are sure that what you want will always be between file: and the first ,, you can use:

wget -O - http://vodlocker.com/embed-wrdlm4dbigu4-850x450.html 2>/dev/null | 
    grep -oP 'file:\K[^,]*'

You need the -O - to tell wget to print the html to standard out instead of saving to a file. This is then piped through grep with Perl Compatible Regular Expressions (-P) and -o to tell it to only print the matched portion of the line. The regex itself looks for file: and then discards it (\K) and then 0 or more non-, characters ([^,]*). The 2>/dev/null discards wget's messages.

Note that in the example URL you gave, there are three matches:

$ wget -O - http://vodlocker.com/embed-wrdlm4dbigu4-850x450.html | grep -oP 'file:\K[^,]*'
"http://77.81.98.228:8777/n2ceexa2lo4pcnokaldsf4o64qg7le7rp2xxamcxtdebwqbtaozwssfetq/v.mp4"
"http://vodlocker.com/dl?op=get_slides&file_code=wrdlm4dbigu4"
"http://vodlocker.com/images/vodjw_logo.png"

If you know you only need the first one, use -m 1 to tell grep to stop after the first match:

$ wget -O - http://vodlocker.com/embed-wrdlm4dbigu4-850x450.html 2>/dev/null | 
    grep -m1 -oP 'file:\K[^,]*'
"http://77.81.98.228:8777/n2ceexa2lo4pcnokaldsf4o64qg7le7rp2xxamcxtdebwqbtaozyasfetq/v.mp4"
0
0

Using grep with PCRE (-P):

grep -Po 'file:\s"\K[^"]+\.mp4(?=",)' file.txt

Using sed:

sed -nr 's/.*file: "([^"]+\.mp4)",/\1/p' file.txt

Both finds the desired string between file " and ",, also we need to match .mp4 just before ", so that other unwanted strings do not show up in the output.

Example:

% wget -q -O- http://pastebin.com/raw/eQFTp0cy | grep -Po 'file:\s"\K[^"]+\.mp4(?=",)' 
http://77.81.98.228:8777/n2ceexa2lo4pcnokaldsf4o64qg7le7rp2xxamcxtdebx4vta3lae5iz6e/v.mp4


% wget -q -O- http://pastebin.com/raw/eQFTp0cy | sed -nr 's/.*file: "([^"]+\.mp4)",/\1/p'
http://77.81.98.228:8777/n2ceexa2lo4pcnokaldsf4o64qg7le7rp2xxamcxtdebx4vta3lae5iz6e/v.mp4

You must log in to answer this question.