找到你要的答案

Q:Bash remove line with zero value

Q:Bash零值删除线

I have a file which is 1500 lines long containing cpu values, but somehow there is 0 very second line. Is it possible to remove every second line containing only 0 in a bash command ?

8
0
9
0
10
0
10
0
8
0
9
0

我有一个文件,它是1500行长,包含CPU值,但不知何故有0非常第二行。可以删除在bash命令每一个第二行只含0?

8
0
9
0
10
0
10
0
8
0
9
0
answer1: 回答1:

Remove all zeros

Use grep -v:

-v, --invert-match
             Selected lines are those not matching any of the specified patterns.

Command:

grep -v -e "^0$" file

The problem with this is that it will remove lines all '0' lines.

Remove even lines

awk 'NR % 2 != 0' file

In this case, you have to be sure that all even lines are the ones you want to remove.

Remove even lines that are '0'

awk 'NR % 2 != 0 || ! /0/' file

删除所有的零点

使用grep -v:

-v, --invert-match
             Selected lines are those not matching any of the specified patterns.

命令:

grep -v -e "^0$" file

这个问题是,它将删除所有行' 0 '线。

删除线

awk 'NR % 2 != 0' file

在这种情况下,你必须确保所有的行都是你想要删除的。

删除线 that are '0'

awk 'NR % 2 != 0 || ! /0/' file
answer2: 回答2:

If you want to delete every second line, but only if it contains a 0, then this one liner will help:

awk '($0 == 0) && (NR % 2 == 0) { next } { print }' cpu_values.txt

It means, if the input line equals 0, and it is an even line, then skip it. Otherwise, print the line.

如果你想删除每一个第二行,但只有当它包含一个0,然后这一行将帮助:

awk(0美元= = 0)&;&;(NR % 2 = = 0){一} { }的cpu_values.txt打印

这意味着,如果输入行等于0,它是偶数行,然后跳过它。否则,打印行。

bash  file  cut