I have already extracted the tag from the source document using grep but, now I cant seem to figure out how to easily extract the properties from the string. Also I want to avoid having to use any programs that would not usually be present on a standard installation.
$tag='<img src="http://imgs.xkcd.com/comics/barrel_cropped_(1).jpg" title="Don't we all." alt="Barrel - Part 1" />'
I need to end up with the following variables
$src="http://imgs.xkcd.com/comics/barrel_cropped_(1).jpg"
$title="Don't we all."
$alt="Barrel - Part 1"
I went with dacracot's suggestion of using sed although I would have prefered if he had given me some sample code
You can use xmlstarlet. Then, you don't even have to extract the element yourself:
You can even turn this into a function
If you don't want to reparse the document several times, you can also do:
I'm sure there's a better way to remove the last argument to a shell function, but I don't know it.
If xmlstarlet is available on a standard installation and the sequence of src-title-alt does not change, you can use the following code as well:
Since this bubbled up again, there is now my Xidel that has 2 features which make this task trivial:
pattern matching on the xml
exporting all matched variables to the shell
So it becomes a single line: