Delete help

Please give me some hints with awk or something, to delete the lines corresponding to a fixed 2nd field, for which A line is absent.

$ cat file.txt
#Test file

a 1 2232 ert
a 1 679 asd
A 1 12 oio

a 2 131 sd
a 2 56 adsad
a 2 567 sassa
A 2 90 asd

a 4 234 gfg
a 4 566 erer


a 5 233 yu
A 5 232 yuyu

So the required output is something like this for the above file:

#Test file

a 1 2232 ert
a 1 679 asd
A 1 12 oio

a 2 131 sd
a 2 56 adsad
a 2 567 sassa
A 2 90 asd

a 5 233 yu
A 5 232 yuyu

Thank you.

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

awk '/A /' ORS='\n\n' RS= filename

Or better:

perl -00 -nle'/^A/m and print' filename

Thanks radoulov for your reply. Thanks.

Could you also suggest how can I keep any other lines (e.g. in the above file the line "#Test file" or any other intermediate comment lines) in the output.
Thanks.

perl -00 -nle'print unless/^a.*\n^a/im&&!/^A/m' filename

Edit: Just saw you want only the comments, so it depends, you may try something like this:

perl -00 -nle'/^[A#]/m and print' filename

But it will fail on an input like this:

# 
a 4 234 gfg
a 4 566 erer

With AWK you can try something like this, but it will fail in too many situations:

awk '/[A#] */' ORS='\n\n' RS= filename