Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash Grep精确字符串';来自xml文件的_Bash - Fatal编程技术网

Bash Grep精确字符串';来自xml文件的

Bash Grep精确字符串';来自xml文件的,bash,Bash,如何从xml文件中grep精确的单词(字符串)。这是xml文件(输入文件)的一部分: 当我尝试使用grep-E“cellnidentity=“input.xml时,我得到了完整的句子(行),但我只需要如上所述 您可以使用以下正则表达式: grep -oP 'cellIdentity="\d*"' file 您可以使用以下正则表达式: grep -oP 'cellIdentity="\d*"' file 使用grep的-o选项仅获取匹配的图案。使用名为t.txt的文件中的示例: grep -o

如何从xml文件中grep精确的单词(字符串)。这是xml文件(输入文件)的一部分:


当我尝试使用
grep-E“cellnidentity=“input.xml
时,我得到了完整的句子(行),但我只需要如上所述

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

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

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

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

使用
grep
-o
选项仅获取匹配的图案。使用名为
t.txt的文件中的示例:

grep -o 'cellIdentity="[0-9]*"' t.txt 
cellIdentity="42901"
cellIdentity="42905"
cellIdentity="42902"
cellIdentity="42906"
cellIdentity="42903"
cellIdentity="42907"

使用
grep
-o
选项仅获取匹配的图案。使用名为
t.txt的文件中的示例:

grep -o 'cellIdentity="[0-9]*"' t.txt 
cellIdentity="42901"
cellIdentity="42905"
cellIdentity="42902"
cellIdentity="42906"
cellIdentity="42903"
cellIdentity="42907"

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

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

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

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

xmlstarlet sel -t -m "//Cell" -m @cellIdentity -v . -n file.xml
Jordan@workstation:~$ egrep -o "cellIdentity=\"[0-9]{5}\"" ddff 
cellIdentity="42901"
cellIdentity="42905"
cellIdentity="42902"
cellIdentity="42906"
cellIdentity="42903"
cellIdentity="42907"
与grep相比,它的脆弱性要小得多,可以处理更多的XML文件和边缘案例

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个数字

答案的其余部分应包含:)

-o
只输出匹配的字符串,而不是整行

[0-9]{5}
正在查找恰好出现的5个数字


答案的其余部分包含预期的:)

!!对
-o
标志和正则表达式结构的解释将有助于回答这个问题。添加这些将改变这一点,从给出答案到教授如何解决这个问题。这个答案将受益于对
-o
标志和正则表达式结构的解释。加上这些将改变这一点,从给出答案到教授如何解决这个问题。只是澄清一下:这不是一个标准的实用程序。OSX用户:可以使用标准的
xmllint
实用程序:
xmllint--xpath'//Cell/@cellnidentity'file.xml | sed's//;s/\'$'\n'/g'
只是为了澄清:不是标准的实用程序。OSX用户:可以使用标准的
xmllint
实用程序:
xmllint--xpath'//Cell/@cellnidentity'file.xml | sed's//;s/\'$'\n'/g'
只是一个提示:
-P
(用于支持Perl兼容的正则表达式)在所有平台(例如OSX)上都不可用。只是一个提示:
-P
(用于支持Perl兼容的正则表达式)在所有平台(例如OSX)上都不可用。