Unix命令&正则表达式从语料库中删除XML标记

时间:2017-09-15 22:00:34

标签: regex unix grep tokenize

我使用文本语料库,我编写了以下Unix命令和正则表达式。

我想在没有XML 的情况下仅为英文段 提取文本,并将其放入名为“file.txt”的文件中。

以下代码仅删除<seg>,但它保留结束XML标记</seg>。查看输入和输出以了解我的问题。

cat uncorpora_plain.txt |grep -a1 '<tuv xml:lang="EN">' |grep '<seg>' |perl -pe 's/\<seg>\b/''/'

提取前的部分文字:

  <tuv xml:lang="EN">
    <seg>Adopted at the 81st plenary meeting, on 4 December 2000, on 
   the recommendation of the Committee (A/55/602/Add.2 and Corr.1, 
   para. 94), by a recorded vote of 106 to 1, with 67 abstentions, as 
   follows:</seg>

运行Unix命令后的输出:

    Adopted at the 81st plenary meeting, on 4 December 2000, on the 
 recommendation of the Committee (A/55/602/Add.2 and Corr.1, para. 94), 
 by a recorded vote of 106 to 1, with 67 abstentions, as follows:</seg>

非常感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

sed -e 's/<[^>]*>//g' file.xml

这应该有效

答案 1 :(得分:1)

我会重复这个陈腐的规则: 不用awk / sed / grep解析xml / html - 使用适当的解析器。

xmlstarlet就是其中之一。

有效的XML示例:

<root>
 <tuv xml:lang="EN">
    <seg>Adopted at the 81st plenary meeting, on 4 December 2000, on 
   the recommendation of the Committee (A/55/602/Add.2 and Corr.1, 
   para. 94), by a recorded vote of 106 to 1, with 67 abstentions, as 
   follows:</seg>
</tuv>
 <tuv xml:lang="UA">
    <seg>УкраÏна - унікальна країна,
     багата талановитими людьми ...</seg>
</tuv>
</root>

命令:

xmlstarlet sel -t -v "//tuv[@xml:lang='EN']//seg" -n input.xml > uncorpus.eng.txt

uncorpus.eng.txt内容:

Adopted at the 81st plenary meeting, on 4 December 2000, on 
   the recommendation of the Committee (A/55/602/Add.2 and Corr.1, 
   para. 94), by a recorded vote of 106 to 1, with 67 abstentions, as 
   follows:

答案 2 :(得分:0)

听起来这就是你要求的(使用GNU awk进行多字符RS):

awk -v RS='</seg>' 'sub(/.*<tuv\s+xml:lang="EN">\s*<seg>/,"")'

但没有可测试的样本输入/输出,这是一个猜测。这是针对输入的@RomanPerekhrest输入FWIW:

$ cat file
<root>
 <tuv xml:lang="EN">
    <seg>Adopted at the 81st plenary meeting, on 4 December 2000, on
   the recommendation of the Committee (A/55/602/Add.2 and Corr.1,
   para. 94), by a recorded vote of 106 to 1, with 67 abstentions, as
   follows:</seg>
</tuv>
 <tuv xml:lang="UA">
    <seg>УкраÏна - унікальна країна,
     багата талановитими людьми ...</seg>
</tuv>
</root>

$ awk -v RS='</seg>' 'sub(/.*<tuv\s+xml:lang="EN">\s*<seg>/,"")' file
Adopted at the 81st plenary meeting, on 4 December 2000, on
   the recommendation of the Committee (A/55/602/Add.2 and Corr.1,
   para. 94), by a recorded vote of 106 to 1, with 67 abstentions, as
   follows:

如果你想摆脱每行开头的空格:

$ awk -v RS='</seg>' 'sub(/.*<tuv\s+xml:lang="EN">\s*<seg>/,""){ gsub(/\n[[:blank:]]*/,"\n"); print}' file
Adopted at the 81st plenary meeting, on 4 December 2000, on
the recommendation of the Committee (A/55/602/Add.2 and Corr.1,
para. 94), by a recorded vote of 106 to 1, with 67 abstentions, as
follows: