I am grepping an XML File, which gives me output like this:
我正在加载一个XML文件,它会给我这样的输出:
<tag>data</tag>
<tag>more data</tag>
...
Note, this is a flat file, not an XML tree. I want to remove the XML tags and just display the data in between. I'm doing all this from the command line and was wondering if there is a better way than piping it into awk twice...
注意,这是一个平面文件,不是XML树。我想删除XML标记,只显示中间的数据。我在命令行中完成所有这些工作,我想知道是否有更好的方法可以将它导入两次awk……
cat file.xml | awk -F'>' '{print $2}' | awk -F'<' '{print $1}'
Ideally, I would like to do this in one command
理想情况下,我希望在一个命令中完成此操作
5 个解决方案
#1
32
If your file looks just like that, then sed
can help you:
如果您的文件是这样的,那么sed可以帮助您:
sed -e 's/<[^>]*>//g' file.xml
Of course you should not use regular expressions for parsing XML because it's hard.
当然,您不应该使用正则表达式来解析XML,因为这很难。
#2
4
Using awk:
使用awk:
awk '{gsub(/<[^>]*>/,"")};1' file.xml
#3
1
Give this a try:
给这一个尝试:
grep -Po '<.*?>\K.*?(?=<.*?>)' inputfile
Explanation:
解释:
Using Perl Compatible Regular Expressions (-P
) and outputting only the specified matches (-o
):
使用Perl兼容的正则表达式(-P)并输出仅指定的匹配(-o):
-
<.*?>
- Non-greedy match of any characters within angle brackets - < . * ?> -尖括号内任何字符的非贪婪匹配
-
\K
- Don't include the preceding match in the output (reset match start - similar to positive look-behind, but it works with variable-length matches) - \K -不要在输出中包含前面的匹配项(重置匹配开始——类似于正的查找延迟,但它适用于可变长度的匹配项)
-
.*?
- Non-greedy match stopping at the next match (this part will be output) - . * ?-下一个匹配停止非贪心匹配(此部分为输出)
-
(?=<.*?>)
- Non-greedy match of any characters within angle brackets and don't include the match in the output (positive look-ahead - works with variable-length matches) - (?=<.*?>) -尖括号内任何字符的非贪婪匹配,且不包括输出中的匹配(正向前查找-适用于变长匹配)
#4
1
Use html2text
command-line tool, which converts html into plain text.
使用html2text命令行工具,它将html转换为纯文本。
Alternatively you may try ex-way:
你也可以尝试前路:
ex -s +'%s/<[^>].\{-}>//ge' +%p +q! file.txt
or:
或者:
cat file.txt | ex -s +'%s/<[^>].\{-}>//ge' +%p +q! /dev/stdin
#5
0
I know this is not a "perlgolf contest", but I used to use this trick.
我知道这不是一场“高尔夫比赛”,但我曾经用过这个技巧。
Set Record Separator for <
or >
, then print only odd lines:
为 <或> 设置记录分隔符,然后只打印奇数行:
awk -vRS='<|>' NR%2 file.xml
#1
32
If your file looks just like that, then sed
can help you:
如果您的文件是这样的,那么sed可以帮助您:
sed -e 's/<[^>]*>//g' file.xml
Of course you should not use regular expressions for parsing XML because it's hard.
当然,您不应该使用正则表达式来解析XML,因为这很难。
#2
4
Using awk:
使用awk:
awk '{gsub(/<[^>]*>/,"")};1' file.xml
#3
1
Give this a try:
给这一个尝试:
grep -Po '<.*?>\K.*?(?=<.*?>)' inputfile
Explanation:
解释:
Using Perl Compatible Regular Expressions (-P
) and outputting only the specified matches (-o
):
使用Perl兼容的正则表达式(-P)并输出仅指定的匹配(-o):
-
<.*?>
- Non-greedy match of any characters within angle brackets - < . * ?> -尖括号内任何字符的非贪婪匹配
-
\K
- Don't include the preceding match in the output (reset match start - similar to positive look-behind, but it works with variable-length matches) - \K -不要在输出中包含前面的匹配项(重置匹配开始——类似于正的查找延迟,但它适用于可变长度的匹配项)
-
.*?
- Non-greedy match stopping at the next match (this part will be output) - . * ?-下一个匹配停止非贪心匹配(此部分为输出)
-
(?=<.*?>)
- Non-greedy match of any characters within angle brackets and don't include the match in the output (positive look-ahead - works with variable-length matches) - (?=<.*?>) -尖括号内任何字符的非贪婪匹配,且不包括输出中的匹配(正向前查找-适用于变长匹配)
#4
1
Use html2text
command-line tool, which converts html into plain text.
使用html2text命令行工具,它将html转换为纯文本。
Alternatively you may try ex-way:
你也可以尝试前路:
ex -s +'%s/<[^>].\{-}>//ge' +%p +q! file.txt
or:
或者:
cat file.txt | ex -s +'%s/<[^>].\{-}>//ge' +%p +q! /dev/stdin
#5
0
I know this is not a "perlgolf contest", but I used to use this trick.
我知道这不是一场“高尔夫比赛”,但我曾经用过这个技巧。
Set Record Separator for <
or >
, then print only odd lines:
为 <或> 设置记录分隔符,然后只打印奇数行:
awk -vRS='<|>' NR%2 file.xml