如何使用 shell 脚本提取 xml 属性? [英] How do I extract xml properties using shell scripts?
问题描述
我已经使用 grep 从源文档中提取了标签,但是,现在我似乎无法弄清楚如何轻松地从字符串中提取属性.此外,我想避免使用标准安装中通常不存在的任何程序.
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" />'
我需要得到以下变量
$src="http://imgs.xkcd.com/comics/barrel_cropped_(1).jpg"
$title="Don't we all."
$alt="Barrel - Part 1"
推荐答案
您可以使用 xmlstarlet.然后,您甚至不必自己提取元素:
You can use xmlstarlet. Then, you don't even have to extract the element yourself:
$ echo $tag|xmlstarlet sel -t --value-of '//img/@src'
http://imgs.xkcd.com/comics/barrel_cropped_(1).jpg
你甚至可以把它变成一个函数
You can even turn this into a function
$ get_attribute() {
echo $1 | xmlstarlet sel -t -o """ -v $2 -o """
}
$ src=get_attribute $tag '//img/@src'
如果不想多次重新解析文档,也可以这样做:
If you don't want to reparse the document several times, you can also do:
$ get_values() {
eval file=\${$#}
eval $#=
cmd="xmlstarlet sel "
for arg in $@
do
if [ -n $arg ]
then
var=${arg%%\=*}
expr=${arg#*=}
cmd+=" -t -o \"$var="\" -v $expr -o \""\" -n"
fi
done
eval $cmd $file
}
$ eval $(get_values src='//img/@src' title='//img/@title' your_file.xml)
$ echo $src
http://imgs.xkcd.com/comics/barrel_cropped_(1).jpg
$ echo $title
Don't we all.
我确信有更好的方法来删除 shell 函数的最后一个参数,但我不知道.
I'm sure there's a better way to remove the last argument to a shell function, but I don't know it.
这篇关于如何使用 shell 脚本提取 xml 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!