{
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
} <inputfile >outputfile
То же в остроте жемчуга
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
и играл в гольф
perl -nle's/^(ssh|##|(SERVER))/$2&&$i++/e&&next;$i&&print$i;$i=!print}{$i&&print$i' inputfile >outputfile
Эта версия считает все строки, которые не соответствуют regexp в grep
строка.
#! /usr/bin/perl
# set the Input Record Separator (man perlvar for details)
$/ = '####################';
while(<>) {
# split the rows into an array
my @rows = split "\n";
# get rid of the elements we're not interested in
@rows = grep {!/^#######|^ssh-|^$/} @rows;
# first row of array is the title, and "scalar @rows"
# is the number of entries, so subtract 1.
if (scalar(@rows) gt 1) {
print "$rows[0]\n", scalar @rows -1, "\n"
}
}
Вывод:
Bala Bela;XXXXXX12345;XXXXXX12345678;A 4 Ize Jova;XXXXXX12345;XXXXXX12345;A 3
Если Вы только хотите считать строки, начинающиеся с 'СЕРВЕРА', то:
#! /usr/bin/perl
# set the Input Record Separator (man perlvar for details)
$/ = '####################';
while(<>) {
# split the rows into an array
my @rows = split "\n";
# $rows[0] will be same as $/ or '', so get title from $rows[1]
my $title = $rows[1];
my $count = grep { /^SERVER/} @rows;
if ($count gt 0) {
print "$title\n$count\n"
}
}
sed -n ':a /^SERVER/{g;p;ba}; h' file | uniq -c |
sed -r 's/^ +([0-9]) (.*)/\2\n\1/'
Вывод:
Bala Bela;XXXXXX12345;XXXXXX12345678;A
4
Ize Jova;XXXXXX12345;XXXXXX12345;A
3
Если снабженное префиксом количество в порядке:
sed -n ':a /^SERVER/{g;p;ba}; h' file |uniq -c
Вывод:
4 Bala Bela;XXXXXX12345;XXXXXX12345678;A
3 Ize Jova;XXXXXX12345;XXXXXX12345;A
awk
альтернатива:
/^#{15,}/ { # if line starts with 15 or more number signs
if(k) { # if any key found
print k RS n # print it and occurrences of SERVER
n=0
}
getline # key is on the next line
k = $0
next # move to next record
}
/SERVER/ { n++ } # count occurrences of SERVER
END { print k RS n } # print last record
Все на одной строке:
awk '/^#{15,}/ { if(n>0) { print k RS n; n=0 }; getline; k = $0; next } /SERVER/ { n++ } END { print k RS n }'
Таким образом, Если вывод уже отсортирован в каждом "блоке", Вы могли бы непосредственно применить uniq с проверкой только первых символов N:
cat x | uniq -c -w6
Вот N == 6, поскольку СЕРВЕР состоит из 6 символов с начала строки. Вы закончите с этим выводом (который несколько отличается от Вашего необходимого вывода):
1 ####################
1 Bala Bela;XXXXXX12345;XXXXXX12345678;A
4 SERVER345Z3.DOMAIN.com0
1 ssh-dss ...pubkeyhere...
1 ####################
1 Ize Jova;XXXXXX12345;XXXXXX12345;A
3 SERVER342Z3.DOMAIN.com0
1 ssh-rsa ...pubkeyhere...