Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
Do you require this to be standalone Bash, or are other tools acceptable (grep, awk, perl...)? –  ire_and_curses Sep 14 '12 at 6:50
    
i would assume (and have done so :) that, unless explicitly stated otherwise, a bash scripting question allows all the standard tools like grep, awk, sed, perl and all the rest. –  cas Sep 14 '12 at 10:15

5 Answers 5

up vote 6 down vote accepted
{
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

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
share|improve this answer
    
wow. perl is amazing :D –  gasko peter Sep 14 '12 at 10:57

This version counts all rows that don't match the regexp in the grep line.

#! /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"
    }
}

Output:

Bala Bela;XXXXXX12345;XXXXXX12345678;A
4
Ize Jova;XXXXXX12345;XXXXXX12345;A
3

If you only want to count lines beginning with 'SERVER', then:

#! /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"
    }
}
share|improve this answer

An awk alternative:

/^#{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

All on one line:

awk '/^#{15,}/ { if(n>0) { print k RS n; n=0 }; getline; k = $0; next } /SERVER/ { n++ } END { print k RS n }'
share|improve this answer
sed -n ':a /^SERVER/{g;p;ba}; h' file | uniq -c | 
  sed -r 's/^ +([0-9]) (.*)/\2\n\1/'

Output:

Bala Bela;XXXXXX12345;XXXXXX12345678;A
4
Ize Jova;XXXXXX12345;XXXXXX12345;A
3

If a prefixed count is ok:

sed -n ':a /^SERVER/{g;p;ba}; h' file |uniq -c

Output:

  4 Bala Bela;XXXXXX12345;XXXXXX12345678;A
  3 Ize Jova;XXXXXX12345;XXXXXX12345;A
share|improve this answer

So If the output is already sorted in each "bucket" you could directly apply uniq with checking only the first N characters:

cat x | uniq -c -w6

Here is N==6 as SERVER consists of 6 characters from the beginning of the line. You will end up with this output (which is a bit different from your required output):

  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...
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.