Artificial intelligent assistant

How to count rows ordered by the first field in bash Here is a snippet from the INPUT: ... #################### Bala Bela;XXXXXX12345;XXXXXX12345678;A SERVER345Z3.DOMAIN.com0 SERVER346Z3.DOMAIN.com0 SERVER347Z3.DOMAIN.com0 SERVER348Z3.DOMAIN.com0 ssh-dss ...pubkeyhere... #################### Ize Jova;XXXXXX12345;XXXXXX12345;A SERVER342Z3.DOMAIN.com0 SERVER343Z3.DOMAIN.com0 SERVER345Z3.DOMAIN.com0 ssh-rsa ...pubkeyhere... ... And here is a snippet from the OUTPUT that I need: Bala Bela;XXXXXX12345;XXXXXX12345678;A 4 Ize Jova;XXXXXX12345;XXXXXX12345;A 3 So I need an OUTPUT from the INPUT, so that I could see that how many rows starting with "SERVER" goes to given user (ex.: "Bala Bela;XXXXXX12345;XXXXXX12345678;A"). How can I do this in bash?

{
i=0
while IFS= read -r line; do
case "$line" in
ssh*|'##'*)
;;
SERVER*)
((++i))
;;
*)
if ((i>0)); then echo $i;i=0; fi
echo "$line"
;;
esac
done
if ((i>0)); then echo $i;i=0; fi
} outputfile


The same in perl one-liner


perl -nle '
BEGIN{$i=0}
next if/^(ssh|##)/;
if(/^SERVER/){++$i;next}
print$i if$i>0;
$i=0;
print;
END{print$i if$i>0}' inputfile >outputfile


and golfed


perl -nle's/^(ssh|##|(SERVER))/$2&&$i++/e&&next;$i&&print$i;$i=!print}{$i&&print$i' inputfile >outputfile

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy a8e50b440f60e3cb6aa4681dda309c31