0

I am trying hard to get the output as I Like.

Current Output:

###Server1###
2
###Server2###
0
###Server3###
     5
###Server4###
     0

Required Output:

###Server1###
2
###Server3###
     5

All I am looking is to grep and ignore any line and the previous line that containts 0 (zero) in any place of the line. I am using bash shell.

Barmar
  • 741,623
  • 53
  • 500
  • 612

2 Answers2

0

This is a possible approach:

$ grep -B 1 "^\s*[1-9]$" file
###Server1###
2
--
###Server3###
     5

To get rid of the group separator, we can also do:

$ grep --no-group-separator -B 1 "^\s*[1-9]$" file
###Server1###
2
###Server3###
     5

Explanation

Instead of using grep -v to find the inverse, I think it is easier to look for the lines having a single digit value not being 0. This is done with the "^\s*[1-9]$" expression, that allows spaces before the digit.

With -B 1 we make it print also the line before the matched one.

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
0

Code for GNU :

sed '$!N;/\s*\b0\b\s*/d' file
$ sed '$!N;/\s*\b0\b\s*/d' file
###Server1###
2
###Server3###
     5
captcha
  • 3,756
  • 12
  • 21