printing between two expressions using AWK

Hi,

I am trying to print lines between two expressions using AWK in the following manner
eg

ABB 10 10 20 10
10 10 10 10 10 10
11 11 11 11 11 11
11 11 11 11 11 11
ACC 10 10 10 10

awk '/ABB/,/ACC/' datafile > output. However, in my data file there are a number of occurrences of ABB and ACC; so my question is, how can I print lines, say between the forth occurrence ABB and fourth occurrence of ACC?

Any suggestions would be greatly appreciated. :slight_smile:

Pauli

can u give an example of how the output should look like. Also do u want it to run only in shell/awk or perl is also ok ?

Hi the output would be something like,

ABBA 44 44 44 44 44
L -.02 2.01 3.6
P 6.2 3.2 56 22.8
J 2.5 2.2 3.3
ACCA 44 44 44 44 44
AJJA 44 44 44 44 44
L 2.2 .2.0 2.2
P 3.3 3.2 3.3
J 2.2 .2.2 3.9
DFT 44 44 44 44 44
ABBA 44 44 44 44 44
J 3.3 2.2 2.2
P 6.8 -1.5 5.8
J-.32 .25 .32
ACCA 44 44 44 44 44

Where here, for example, I need to print whats between the second occurrence of ABBA and ACCA. I Guess Awk/Shell.

Many Thanks.

awk '
   /ABB/{n++}
  n==4{flag=1}
  flag==1{print}
  /ACC/ && flag==1 {exit}
' filename

perl:

undef $/;
open $fh,"<","a.txt";
my $str=<$fh>;
my $n=4;
my @arr=split(/(ABB.*\n|ACC.*\n)/,$str);
print join "",@arr[1+($n-1)*4..3+($n-1)*4];

Thank you very much for your post Summer_Chrery :slight_smile: