-1

I would like to count the number of the lines found between each two consecutive "*" characters including one of them. Input:

>*1
AA
BB
CC
>*2
RR
FGRE
>*3
A

output:

4 >*1
3 >*2
2 >*3

where the first field is the number of the lines between *'s and the second field is the string including them.

ahb65
  • 191
  • 2
  • 13

3 Answers3

2

Using awk you can do:

awk '/\*/{if (p) print c, p; p=$0;c=0} {c++} END{if (p) print c, p}' file
4 >*1
3 >*2
2 >*3
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

try this:

cat  x1.txt | perl -ne '
  chomp;
  if ( $_ =~ /^>\*/ ) {
    $l1{$_}++;
  };
  $prev=$_;
  while(<>){
    chomp;
    if ( $_ =~ /^>\*/ ) {
       $prev=$_;
       $l1{$prev}++;
    } else { 
       $l1{$prev}++;}
    };             
    if (eof) {
      foreach $m1  ( keys %l1 ){
        print qq($m1 , $l1{"$m1"}\n);
      };
 };'
michael501
  • 1,452
  • 9
  • 18
1

Here's my command:

nl | sed -n '/*/p;/*/{1!p};$p' | xargs -n4 | awk '{print $3-$1, $2}'
Vytenis Bivainis
  • 2,308
  • 21
  • 28