简体   繁体   English

使用Shell脚本查找在某些条件下xml中存在的多个属性

[英]Finding multiple attributes present in an xml with certain conditions using shell scripting

grep -R --include="*.xml" "Frontal Face" /home/ashutosh/Desktop/imgdone | grep -v "Non Frontal Face"

This command gives me all xmls where "Frontal Face" is present. 该命令为我提供了存在“正面”的所有xml。 If I want to search more attributes through this shell script like "Happy", "Young" (occuring in the xml), what changes shall I do in this? 如果我想通过此Shell脚本搜索更多属性,例如“ Happy”,“ Young”(出现在xml中),我应该对此做哪些更改?

In summary, how to search for multiple attributes? 综上所述,如何搜索多个属性?

As already pointed out in the answers and comments to your previous question , there is nothing here which supports specifically searching for XML attributes. 正如您对上一个问题的答案和注释中已经指出的那样,这里没有任何内容支持专门搜索XML属性。 grep is not a good tool for searching structured formats, as already pointed out numerous times . 正如已经多次指出的那样, grep并不是搜索结构化格式的好工具。

If your question is simply "how do I find X, Y, or Z anywhere in a file", that's trivial; 如果您的问题只是“如何在文件的任何位置找到X,Y或Z”,那么这很简单; grep -E 'X|Y|Z' files . grep -E 'X|Y|Z' files But this does nothing to exclude matches which are not in XML attributes, or to find matches which are somehow obscured by the features of XML (such as X also being validly representable as X or a number of variants of this). 但这并不能排除不属于XML属性的匹配项,也不能找到由于某种原因而被XML功能遮盖的匹配项(例如X也可以有效地表示为X或此类型的许多变体)。 Again, this was already pointed out to you previously. 再一次,这之前已经向您指出过。

While an XPath expression could be written to say "find any element in the parsed element tree with an attribute whose value is 'baz' or 'quux'", that's not really a sane requirement. 虽然可以将XPath表达式写为“在解析的元素树中使用属性值为'baz'或'quux'查找任何元素”,但这并不是真正的明智要求。 Usually, you'd want something like "find any foo element in the tree whose bar attribute is 'baz' or 'quux', ie matches the regular expression ^(baz|quux)$ " which is 通常,您希望使用类似“在树中找到bar属性为'baz'或'quux'的任何foo元素,即匹配正则表达式^(baz|quux)$内容,例如

//foo/@bar[matches(.,'^(baz|quux)$')]

The matches() predicate is an XPath 2.0 feature. matches()谓词是XPath 2.0的功能。

You'd use it something like 你会用它像

find /home/ashutosh/Desktop/imgdone -type f -name '*.xml' -exec \
    xmllint --xpath '//foo/@bar[matches(., "^(baz|quux)$")]' {} \;

If your shell is new enough, you can drop the find command and just use a recursive wildcard like /home/ashutosh/Desktop/imgdone/**/*.xml as the file name argument to xmllint . 如果你的shell是够新,可以删除find命令,并且只使用递归通配符像/home/ashutosh/Desktop/imgdone/**/*.xml作为文件名参数xmllint

If you don't have xmllint , look for xmlstarlet or really any other XPath tool; 如果您没有xmllint ,请寻找xmlstarlet或其他任何XPath工具; there is no single ubiquitous standard utility for this (yet). 目前还没有一个普遍存在的标准实用程序。

If you need to search specific elements like attributes or values within your xml structure, xsltproc or xmllint are the way to go, as tripleee has pointed out. 正如Tripleee指出的那样,如果您需要在xml结构中搜索诸如属性或值之类的特定元素,则可以使用xsltprocxmllint

If you just need some "give lines with X or Y" in the same sentence, you can do something like this: 如果您只需要在同一句子中添加一些“给X或Y的行”,则可以执行以下操作:

grep -E 'X|Y' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM