Search code examples
bashfilecut

Bash remove line with zero value


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

Solution

  • 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