Solved

Linux single field extract from HTML

Posted on 2013-12-27
2
277 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
Comment Utility
Try this
sed -ne '/src/'p filename  | sed 's/.*src=[^\/]*\([^ "]*\).*/\1/'

Open in new window

0
 

Author Closing Comment

by:brothertom
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now