count longest streak of string occurrences

Start with a file that has a bunch of numbers like this:

# tail -20 filename
0
0
1
0
3
0
0
54
0
1
1
2
0
0
0
1
21
90
0
0

We want the longest streak of 0s

# awk '{if($1==0)j++;else j=0; print j}' filename | sort -nu | tail -1
7

One thought on “count longest streak of string occurrences

Leave a Reply

Your email address will not be published. Required fields are marked *