如何计算bash中第一个字段排序的行


9

这是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...
...

这是我需要的输出片段:

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

因此,我需要从INPUT输出,以便可以看到以“ SERVER”开头的行分配给给定用户(例如:“ Bala Bela; XXXXXX12345; XXXXXX12345678; A”)。我该如何在bash中做到这一点?


您是否要求它是独立的Bash,还是可以接受其他工具(grep,awk,perl ...)?
ire_and_curses 2012年

我会假设(并且已经这样做了:),除非另有明确说明,否则bash脚本问题允许所有标准工具,例如grep,awk,sed,perl以及所有其他工具。
cas 2012年

Answers:


6
{
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一线式相同

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

哇。perl很棒:D
gasko peter 2012年

5

此版本会计算该行中与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"
    }
}

输出:

巴拉贝拉; 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"
    }
}

5
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

4

一种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 }'

2

因此,如果输出已经在每个“存储桶”中进行了排序,则可以直接应用uniq并仅检查前N个字符:

cat x | uniq -c -w6

这是N == 6,因为SERVER从行首开始包含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...
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.