extract lines based on few conditions

Hi,

I need to extract lines based on some conditions as explained below:

File format details:
notes:

  1. each set starts with AAA only
  2. number of columns is fixed
  3. number of rows per set may vary (as one set is upto DDD - 4 rows)

Now, if any BBB's 5th column is blank then then script should provide us it's AAA line.

For. e.g, i have data for 3 sets and file has the below:

AAA,1,a,b,c,d
CCC,1,p,q,r,s
BBB,1,j,k,l,m
AAA,2,j,k,l,m
BBB,2,a,b,c,d
AAA,3,w,x,y,z
CCC,3,p,q,r,s
DDD,3,a,b,c,d
BBB,3,j,k,,m

then the output must be

AAA,3,w,x,y,z

I could extract the line that is missing by below:

gawk -F"," '$1 == "BBB" && $5 == "" { print $0 }' /tmp/file

but clueless how to get the AAA-line of it.

Please advise.

Thanks
Prvn

Use nawk or /usr/xpg4/bin/awk on Solaris:

awk -F, '/^AAA/ { _ = $0 }
/^BBB/ && !$5 { print _; _ = "" }
' infile

If the 5th field may contain 0 you should use:

$5==""

instead of:

!$5

Thanks radoulov,

Your Solution worked like a charm.

Could you please tell me what does _ = "" [ after { print _; ] do?

Thanks
Prvn

Given your input format it's unnecessary.
You can remove it :slight_smile:

awk -F, '/^AAA/ { _ = $0 }
/^BBB/ && !$5 { print _ }
' infile
awk -F"," '{
if($1=="AAA")
        tmp=$0
if($1=="BBB" && $5=="")
        print tmp
}' filename