Parsing output

I need to parse the following out put and determine if the USB is a DISK and whether or not it's External.

If an HBA line contains "USB" then does the next line contain
"DISK" and "External".

0:0,31,0: HBA : (aacraid,1) AAC SCSI
0,0,0: DISK : Adaptec ASR4800SAS Volu0001
0,1,0: DISK : Adaptec ASR4800SAS Volu0001
2:0,2,0: HBA : (ide,2) Generic IDE/ATAPI
0,0,0: DISK : TANDBERGRDX 0031
4:0,7,0: HBA : (usb_msto,1) USB USB HBA
0,0,0: TAPE : HP C1537A XU5A
5:0,7,0: HBA : (usb_msto,2) USB USB HBA #< If this line has "usb"
0,0,0: DISK : WD 5000KS External 107a #< 'This External' ?
6:0,2,0: HBA : (ide,1) Generic IDE/ATAPI
0,0,0: CDROM : PIONEER BD-ROM BDC-202 1.01

Thanks

awk '$0 ~ /DISK .* External/ {printf("%s\n%s\n", prev, $0)} 1 {prev=$0}' file

So if I under stand, if $0 matches then it will print $0 and the previous line. Correct?

How could I test if $0 matches DISK .* External and previous match USB then print?

Thanks for your help, much appreciated.

Should it match "USB" or "usb" or does the case not matter??

awk '$0 ~ /DISK .* External/ && tolower(prev) ~ /usb/ {printf("%s\n%s\n", prev, $0)} 1 {prev=$0}' file

Excellent, that does it short and sweet. I don't think case matters but it doesn't hurt to make it lower.

I'm trying to get use to the shorter awk statements, mine are always so lengthy.

I'm trying to understand how the 'prev' is working or which portion of the statement actually is pointing to the previous line. Is that something built into awk?

Thanks again

The script has two parts to it...one part saves every line in the variable "prev"

1 {prev=$0}

The other part prints the previous "prev" and current "$0" lines if the logical AND of two conditions listed below is true.
Check whether the current line aka "$0" contains the keywords "DISK" and "External" AND (&&) if the previous line aka "prev" contains the keyword "USB"

/DISK .* External/ && tolower(prev) ~ /usb/ {printf("%s\n%s\n", prev, $0)}

Ok ok, I got it now. I think my brain is a little slow today...being Friday and all :slight_smile:

Thanks for the explaination, once again much appreciated.