1

If I have a line of text which looks like...

[garbage] <img src="[some url]" [garbage] /> [garbage]

Using sed, how can I get the URL that the image is pointing to? There are no other instances of src= on that line.

Clark Gaebel
  • 17,280
  • 20
  • 66
  • 93

2 Answers2

7
cat html | sed -n 's/.*<img src="\([^"]*\)".*/\1/p'

The -n /p idiom allows you to ignore all the other lines in the file (i.e. do a sed and a grep in one go), while "([^"]*)" just says find the stuff in the quotes.

Conrad Irwin
  • 1,312
  • 8
  • 13
0

Ruby (1.9+)

$ ruby -ne 'puts $_.scan(/img src=\"(.[^"]*)/)' file
kurumi
  • 25,121
  • 5
  • 44
  • 52