如何使用sed来提取子string

我有一个包含以下行的文件:

<parameter name="PortMappingEnabled" access="readWrite" type="xsd:boolean"></parameter> <parameter name="PortMappingLeaseDuration" access="readWrite" activeNotify="canDeny" type="xsd:unsignedInt"></parameter> <parameter name="RemoteHost" access="readWrite"></parameter> <parameter name="ExternalPort" access="readWrite" type="xsd:unsignedInt"></parameter> <parameter name="ExternalPortEndRange" access="readWrite" type="xsd:unsignedInt"></parameter> <parameter name="InternalPort" access="readWrite" type="xsd:unsignedInt"></parameter> <parameter name="PortMappingProtocol" access="readWrite"></parameter> <parameter name="InternalClient" access="readWrite"></parameter> <parameter name="PortMappingDescription" access="readWrite"></parameter> 

我想对这个文件执行命令来只提取参数名称,如下面的输出所示:

 $sedcommand file.txt PortMappingEnabled PortMappingLeaseDuration RemoteHost ExternalPort ExternalPortEndRange InternalPort PortMappingProtocol InternalClient PortMappingDescription 

什么可能是这个命令?

你想要awk

这将是一个快速和肮脏的黑客:

awk -F "\"" '{print $2}' /tmp/file.txt

 PortMappingEnabled PortMappingLeaseDuration RemoteHost ExternalPort ExternalPortEndRange InternalPort PortMappingProtocol InternalClient PortMappingDescription 

grep出生提取的东西:

 grep -Po 'name="\K[^"]*' 

测试你的数据:

 kent$ echo '<parameter name="PortMappingEnabled" access="readWrite" type="xsd:boolean"></parameter> <parameter name="PortMappingLeaseDuration" access="readWrite" activeNotify="canDeny" type="xsd:unsignedInt"></parameter> <parameter name="RemoteHost" access="readWrite"></parameter> <parameter name="ExternalPort" access="readWrite" type="xsd:unsignedInt"></parameter> <parameter name="ExternalPortEndRange" access="readWrite" type="xsd:unsignedInt"></parameter> <parameter name="InternalPort" access="readWrite" type="xsd:unsignedInt"></parameter> <parameter name="PortMappingProtocol" access="readWrite"></parameter> <parameter name="InternalClient" access="readWrite"></parameter> <parameter name="PortMappingDescription" access="readWrite"></parameter> '|grep -Po 'name="\K[^"]*' PortMappingEnabled PortMappingLeaseDuration RemoteHost ExternalPort ExternalPortEndRange InternalPort PortMappingProtocol InternalClient PortMappingDescription 

sed 's/[^"]*"\([^"]*\).*/\1/'

做这个工作。

您不应该使用像sed或awk这样的工具来解析XML。 这是错误的倾向。

如果输入发生变化,在名称参数之前,您将获得换行符而不是空格,它将在某一天失败,从而产生意外的结果。

如果你确定,那么你的输入将总是以这种方式进行格式化,你可以使用cut 。 它比sedawk更快:

 cut -d'"' -f2 < input.txt 

首先解析它会更好,只提取参数名称属性:

 xpath -q -e //@name input.txt | cut -d'"' -f2 

要了解有关xpath的更多信息,请参阅本教程: http : //www.w3schools.com/xpath/