Skip to main content
Ensure that the output is numeric (c+0) even if no lines are counted. Thanks @EdMorton.
Source Link
user232326
user232326

It is possible to match exactly n zeros (not more) with (GNU) sed:

$ i=3; # use the n value wanted. $ sed -E 's/\<0\>/&/'"$i"';Ta;{s//&/'"$((i+1))"';T};:a;d' file | wc -l 7 

Explanation

i=3 # define the count of the regex to match. sed -E # use extended regexes. reduce the need of `\`. /\<0\>/ # match a zero (0) surrounded by whitespace. s/\<0\>/&/ # replace zeros with themselves (detect that they exist) '"$i"' # count how many zeros to change. Ta # branch to label `a` (delete line) if # there were less than `i` zeros {s//&/'"$((i+1))"' # replace again but now i+1 zeros T # branch to print if there are no more zeros than `i`. :a;d # label to delete line (and catch any other line). 

The equivalent with (GNU) awk is even simpler (gsub counts exactly how many zeros were replaced).

$ awk -vn=3 'gsub(/\<0\>/,"&")==3{c++}END{print cc+0}' file 7 

It is possible to match exactly n zeros (not more) with (GNU) sed:

$ i=3; # use the n value wanted. $ sed -E 's/\<0\>/&/'"$i"';Ta;{s//&/'"$((i+1))"';T};:a;d' file | wc -l 7 

Explanation

i=3 # define the count of the regex to match. sed -E # use extended regexes. reduce the need of `\`. /\<0\>/ # match a zero (0) surrounded by whitespace. s/\<0\>/&/ # replace zeros with themselves (detect that they exist) '"$i"' # count how many zeros to change. Ta # branch to label `a` (delete line) if # there were less than `i` zeros {s//&/'"$((i+1))"' # replace again but now i+1 zeros T # branch to print if there are no more zeros than `i`. :a;d # label to delete line (and catch any other line). 

The equivalent with (GNU) awk is even simpler (gsub counts exactly how many zeros were replaced).

$ awk -vn=3 'gsub(/\<0\>/,"&")==3{c++}END{print c}' file 7 

It is possible to match exactly n zeros (not more) with (GNU) sed:

$ i=3; # use the n value wanted. $ sed -E 's/\<0\>/&/'"$i"';Ta;{s//&/'"$((i+1))"';T};:a;d' file | wc -l 7 

Explanation

i=3 # define the count of the regex to match. sed -E # use extended regexes. reduce the need of `\`. /\<0\>/ # match a zero (0) surrounded by whitespace. s/\<0\>/&/ # replace zeros with themselves (detect that they exist) '"$i"' # count how many zeros to change. Ta # branch to label `a` (delete line) if # there were less than `i` zeros {s//&/'"$((i+1))"' # replace again but now i+1 zeros T # branch to print if there are no more zeros than `i`. :a;d # label to delete line (and catch any other line). 

The equivalent with (GNU) awk is even simpler (gsub counts exactly how many zeros were replaced).

$ awk -vn=3 'gsub(/\<0\>/,"&")==3{c++}END{print c+0}' file 7 
Source Link
user232326
user232326

It is possible to match exactly n zeros (not more) with (GNU) sed:

$ i=3; # use the n value wanted. $ sed -E 's/\<0\>/&/'"$i"';Ta;{s//&/'"$((i+1))"';T};:a;d' file | wc -l 7 

Explanation

i=3 # define the count of the regex to match. sed -E # use extended regexes. reduce the need of `\`. /\<0\>/ # match a zero (0) surrounded by whitespace. s/\<0\>/&/ # replace zeros with themselves (detect that they exist) '"$i"' # count how many zeros to change. Ta # branch to label `a` (delete line) if # there were less than `i` zeros {s//&/'"$((i+1))"' # replace again but now i+1 zeros T # branch to print if there are no more zeros than `i`. :a;d # label to delete line (and catch any other line). 

The equivalent with (GNU) awk is even simpler (gsub counts exactly how many zeros were replaced).

$ awk -vn=3 'gsub(/\<0\>/,"&")==3{c++}END{print c}' file 7