1

这是我的示例文件:

<?xml version="1.0" encoding="UTF-8" ?>
 <testjar>
 <testable>
  <trigger>Trigger1</trigger>
  <message->2012-06-14T00:03.54</message>
 <sales-info>
  <san-a>no</san-a>
  <san-b>no</san-b>
  <san-c>no</san-c>
  </sales-info>
  </testable>
  </testjar>

我需要从中提取xml标签-

例如上述文件的输出应该是

testjar
testable
trigger
message
sales-info
....
4

2 回答 2

3
$> cat ./text
<?xml version="1.0" encoding="UTF-8" ?>
 <testjar>
 <testable>
  <trigger>Trigger1</trigger>
  <message>2012-06-14T00:03.54</message>
 <sales-info>
  <san-a>no</san-a>
  <san-b>no</san-b>
  <san-c>no</san-c>
  </sales-info>
  </testable>
  </testjar>

$> grep -P -o "(?<=\<)[^>?/]*(?=\>)" ./text
testjar
testable
trigger
message
sales-info
san-a
san-b
san-c 

正则表达式(?<=\<)[^>?/]*(?=\>)由3部分组成:

  • (?<=\<):(?<=)是后向运算符,所以它的意思是“在<之后”;

  • [^>?/]*: 不是>, ?, /0 次或更多次;

  • (?=\>):(?=)是前瞻运算符,所以它的意思是“之前>”

于 2012-07-25T08:27:13.407 回答
0
awk -F">" '{print $1}' xmlfile | sed -e '/<\//d' -e '/<?/d' -e 's/<//g'
于 2012-07-25T08:44:32.490 回答