Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Linux single field extract from HTML

Posted on 2013-12-27
2
Medium Priority
?
293 Views
Last Modified: 2013-12-28
Hi,

Can someone help out please with a linux command (been trying sed) to extract the src string from the HTML below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
                            <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
... a whole bunch of html including other img tags...
</script>
<img src="/path/to/file/12345667.jpg" alt="" /></body>
</html>

I would like to pipe this HTML file into the command and have the command output just the src of the very last IMG tag in the file (/path/to/file/12345667.jpg in this case), which I will assign to a bash variable for subsequent use.

Very grateful
BT
0
Comment
Question by:brothertom
2 Comments
 
LVL 31

Accepted Solution

by:
farzanj earned 2000 total points
ID: 39742721
Try this
sed -ne '/src/'p filename  | sed 's/.*src=[^\/]*\([^ "]*\).*/\1/'

Open in new window

0
 

Author Closing Comment

by:brothertom
ID: 39743239
Thanks farzanj.

Tiny refinement for the actual file did the trick...

sed -ne '/img src/'p filename  | sed 's/.*src=[^\/]*\([^ "]*\).*/\1/' | tail -1

BT
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question