【发布时间】:2015-03-15 03:14:07
【问题描述】:
我正在使用一个很长的 .XML 文件(大约 60K 行)。我需要 bash 来启动脚本并让用户输入要从 .XML 文件中删除的名称。我在想sed,但如果有更好的选择,我也愿意。这是我到目前为止所得到的:
echo -n "Type media to remove and press [ENTER]"
read TARGET
while true; do
read -p "Are you sure you wish to remove $TARGET from the system?" yn
case $yn in
[Yy]* ) SED COMMAND HERE; break;;
[Nn]* ) echo "Cancelling..."; exit;;
* ) echo "---please answer [Y] or [N]";;
esac
done
这里是 .XML 文件的一部分。请注意,我发布的这一部分在 .XML 中重复了数百次。块中的唯一区别是我在此示例中标记为“corrupt”。
<media>
<name>"corrupt"</name>
<parent>system</parent>
<location>/path/to/the/"corrupt".zip</location>
<video>/another/path/"corrupt".flv</video>
<images>
<image>
<type>saved</type>
<image-file>/yet/another/path/"corrupt".png</image-file>
</image>
</images>
</media>
在本例中,我希望从 .XML 文件中删除“corrupt”。我认为重要的是要说 .XML 文件中只有 1 个“corrupt”实例。此外,对于其他“corrupt_files”,文件名中没有空格,只有下划线或破折号。
所以sed 需要删除包含“corrupt”信息的整个 xml 块,在删除文本的地方不留空行,然后脚本将覆盖当前的“media.xml”文件.
我希望这个问题不会令人困惑。
【问题讨论】:
-
你写'所以 sed 需要删除包含“损坏”信息的整个 xml 块'; “整个 xml 块”是什么意思?您是指
<location>...</location>和<video>...</video>元素,还是指整个<media>...</media>元素?正如@Jubobs 指出的那样,无论哪种方式,sed都可能是该工作的错误工具,但您所指的工具会影响答案。 -
@Brian ...对不起。通过块,我的意思是我发布的整个 xml 示例。这就是我所说的块。因此,包括
... 在内的所有内容 -
@jubobs 我不必使用
sed,这正是我认为可行的方法