Solved

Linux single field extract from HTML

Posted on 2013-12-27
2
287 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 500 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Utilizing an array to gracefully append to a list of EmailAddresses
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…

860 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