简体   繁体   English

使用sed有条件删除xml文件中的行

[英]conditional deletion of lines in xml file using sed

This question may not have a straight forward answer... if so, not to worry. 这个问题可能没有简单的答案...如果是这样,请不要担心。 I have an XML file that looks something like 我有一个看起来像的XML文件

<a>0.00</a>
<b>0.00</b>
<c>0.00</c>
<d>0.00</d>

This is a dumbed down version of what i'm working with. 这是我正在使用的精简版本。 My goal is to delete any line with a data point of zero, if and only if all data points are zero (as the example i've given above). 我的目标是,当且仅当所有数据点均为零时,才删除数据点为零的任何行(如我上面给出的示例)。 Even if one of the lines in the file contains a non zero entry, i can't delete any of them. 即使文件中的某一行包含非零条目,我也无法删除其中任何一条。 For example if the above was 例如,如果上面是

<a>0.00</a>
<b>0.00</b>
<c>1.00</c>
<d>0.00</d>

then the file should remain untouched. 那么该文件应保持不变。 Ultimately, I only want to delete all lines if they all have zero in each and every tag, and leave the file as is otherwise. 最终,如果每个行的每个标记中的行都为零,我只想删除所有行,否则将文件保留为原样。 I know the following will work if all lines have a zero data entry. 我知道如果所有行的数据条目均为零,以下内容将起作用。

sed --in-place '\|>0.00</|d' $FILENAME 

However, if I have the second example below, this will leave me with 但是,如果我有下面的第二个示例,这将使我陷入困境

<c>1.00</c>

which isn't what I'm looking for. 这不是我想要的。 I know I probably need an if statement, something like 我知道我可能需要一个if语句,例如

if (all lines contain the string '>0.00</') then
   sed --in-place '\|>0.00</|d' $FILENAME 
fi

I suppose its the condition in the if statement that i'm not sure about. 我想我不确定的if语句中的条件。 How would I go about stating "if all lines contain the above mentioned string"? 我该如何声明“如果所有行都包含上述字符串”? I dont necessarily have to use sed, I have used awk in the past, but I am most comfortable with sed.... Any input or pointers in the right direction would be most appreciated. 我不必一定要使用sed,我过去曾经使用过awk,但是我对sed最为满意。...在正确方向上的任何输入或指针将不胜感激。 Thanks 谢谢

grep -vq ">0.00<" $FILENAME || sed -i '\|>0.00<|d' $FILENAME

仅在所有行中包含0.00的文件中执行第二个命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM