Sed 删除用户指定的块
Sed to remove user specified block
我正在处理一个很长的 .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" 文件。
我希望这个问题不会令人困惑。
您应该使用正确的 xml
工具,但是此 gnu awk
删除了 name
包含 corrupt
的块
cat file
<media>
<name>"test1"</name>
<parent>system</parent>
<location>/path/to/the/"test1".zip</location>
<video>/another/path/"test1".flv</video>
<images>
<image>
<type>saved</type>
<image-file>/yet/another/path/"test1".png</image-file>
</image>
</images>
</media>
<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>
<media>
<name>"test2"</name>
<parent>system</parent>
<location>/path/to/the/"test2".zip</location>
<video>/another/path/"test2".flv</video>
<images>
<image>
<type>saved</type>
<image-file>/yet/another/path/"test2".png</image-file>
</image>
</images>
</media>
awk -v RS="<media>" '!/<name>"corrupt/ && NR>1 {print RS[=11=]}'
<media>
<name>"test1"</name>
<parent>system</parent>
<location>/path/to/the/"test1".zip</location>
<video>/another/path/"test1".flv</video>
<images>
<image>
<type>saved</type>
<image-file>/yet/another/path/"test1".png</image-file>
</image>
</images>
</media>
<media>
<name>"test2"</name>
<parent>system</parent>
<location>/path/to/the/"test2".zip</location>
<video>/another/path/"test2".flv</video>
<images>
<image>
<type>saved</type>
<image-file>/yet/another/path/"test2".png</image-file>
</image>
</images>
</media>
我正在处理一个很长的 .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" 文件。
我希望这个问题不会令人困惑。
您应该使用正确的 xml
工具,但是此 gnu awk
删除了 name
包含 corrupt
cat file
<media>
<name>"test1"</name>
<parent>system</parent>
<location>/path/to/the/"test1".zip</location>
<video>/another/path/"test1".flv</video>
<images>
<image>
<type>saved</type>
<image-file>/yet/another/path/"test1".png</image-file>
</image>
</images>
</media>
<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>
<media>
<name>"test2"</name>
<parent>system</parent>
<location>/path/to/the/"test2".zip</location>
<video>/another/path/"test2".flv</video>
<images>
<image>
<type>saved</type>
<image-file>/yet/another/path/"test2".png</image-file>
</image>
</images>
</media>
awk -v RS="<media>" '!/<name>"corrupt/ && NR>1 {print RS[=11=]}'
<media>
<name>"test1"</name>
<parent>system</parent>
<location>/path/to/the/"test1".zip</location>
<video>/another/path/"test1".flv</video>
<images>
<image>
<type>saved</type>
<image-file>/yet/another/path/"test1".png</image-file>
</image>
</images>
</media>
<media>
<name>"test2"</name>
<parent>system</parent>
<location>/path/to/the/"test2".zip</location>
<video>/another/path/"test2".flv</video>
<images>
<image>
<type>saved</type>
<image-file>/yet/another/path/"test2".png</image-file>
</image>
</images>
</media>