gpt4 book ai didi

xml - Linux - bash脚本从文件中删除同一行之前的每一行

转载 作者:太空宇宙 更新时间:2023-11-04 05:10:09 24 4
gpt4 key购买 nike

我有一个无效的 xml 文件(它是另一个进程的输出),我需要在 Linux(bash 脚本)中验证它。在无效文件中有空标签 - 没有关闭标签,例如:

<om>
<om>
<some data>
</some data>
</om>

我尝试运行以下命令:

  1. 使用 xsl:
xsltproc prepare_v270.xsl test.xml  > /tmp/test.xml.bak
test.xml:413282: parser error : Excessive depth in document: 256 use XML_PARSE_HUGE option
<om>
^

无法解析test.xml2.使用xmlstarlet

 sudo xmlstarlet ed -d '//*[not(normalize-space())]' test.xml
test.xml:413282.5: Excessive depth in document: 256 use XML_PARSE_HUGE option
<om>
^

等等

我尝试编写一个 bash 脚本来删除每个 <om>行前一行 <om>单词由以下脚本编写:

#!/bin/sh
sed '
/\<om\>/ {
#append the next line
N
# look for "<om>" followed by "<om>"
/\<om\>.*\<om\>/ {
# print
P
# then delete the first line
D
}
}' <old.xml >new.xml

但是这不起作用。

最佳答案

我找到了解决方案 - 如果下一行相同,则删除文件中的一行。此逻辑会删除同一开放标记之前的开放标记。

pre=
while IFS='' read -r line || [[ -n "$line" ]]; do
# echo "Line read from file: $line"
# if line does not contain line pre
if ! { [ -n "$pre" ] && [[ "$line" == "$pre" ]] ; }; then
# echo " <$pre> not found!"
echo "$line"
pre=$line
fi
done <old.xml >new.xml

谢谢大家!!

关于xml - Linux - bash脚本从文件中删除同一行之前的每一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56441485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com