繁体   English   中英

xml文件中的grep确切字符串

[英]Grep exact string's from xml file

我怎么能从xml文件中提取确切的单词(字符串)。 这是xml文件(输入文件)的一部分:

 <Sector sectorNumber="1">
    <Cell cellNumber="1" cellIdentity="42901" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0" />
    <Cell cellNumber="2" cellIdentity="42905" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0" />
  </Sector>
  <Sector sectorNumber="2">
    <Cell cellNumber="1" cellIdentity="42902" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0" />
    <Cell cellNumber="2" cellIdentity="42906" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0" />
  </Sector>
  <Sector sectorNumber="3">
    <Cell cellNumber="1" cellIdentity="42903" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0" />
    <Cell cellNumber="2" cellIdentity="42907" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0" />   
  </Sector>

我想grep所有cellIdentity="..." ,所以基本上它应该像这样

cellIdentity="42901"
cellIdentity="42905"
cellIdentity="42902"
cellIdentity="42906"
cellIdentity="42903"
cellIdentity="42907"

当我尝试使用grep -E "cellIdentity=" input.xml我得到了整个句子(行),但是我只需要如上所述...

使用grep-o选项仅获取匹配的模式。 在示例中,文件名为t.txt

grep -o 'cellIdentity="[0-9]*"' t.txt 
cellIdentity="42901"
cellIdentity="42905"
cellIdentity="42902"
cellIdentity="42906"
cellIdentity="42903"
cellIdentity="42907"
Jordan@workstation:~$ egrep -o "cellIdentity=\"[0-9]{5}\"" ddff 
cellIdentity="42901"
cellIdentity="42905"
cellIdentity="42902"
cellIdentity="42906"
cellIdentity="42903"
cellIdentity="42907"

-o仅输出匹配的字符串,而不输出整个行。

[0-9]{5}正在寻找5个数字。

其余答案包含预期的内容:)

您可以使用以下正则表达式:

grep -oP 'cellIdentity="\d*"' file

要从XML文件提取数据,请使用XML工具:

xmlstarlet sel -t -m "//Cell" -m @cellIdentity -v . -n file.xml

与grep相比,它的脆弱性要小得多,并且可以处理更多XML文件和边缘案例。

暂无
暂无

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

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