awk data filtering

I am trying to filter out some data with awk. If someone could help me that would be great. Below is my input file.

Date: 10-JUN-12 12:00:00
B 0: 00 00 00 00 10 00 16 28
B 120: 00 00 00 39 53 32 86 29
Date: 10-JUN-12 12:00:10
B 0: 00 00 00 00 10 01 11 22
B 120: 00 00 00 29 23 32 16 29
Date: 10-JUN-12 12:00:20
B 0: 00 00 00 00 10 02 17 29
B 120: 00 00 35 51 42 66 14
Date: 10-JUN-12 12:00:30
B 0: 00 00 00 00 10 03 61 42
B 120: 00 00 00 44 33 52 21 52
Date: 10-JUN-12 12:00:40
B 0: 00 00 00 00 10 04 11 22
B 120: 00 00 12 87 10 01 13 42
Date: 10-JUN-12 12:00:50
B 0: 00 00 00 00 10 05 15 24
B 120: 00 00 12 87 10 01 13 42
Date: 10-JUN-12 12:01:00
B 0: 00 00 00 00 10 06 11 22
B 120: 00 00 12 87 10 01 13 42

The above input file is one block of date (i.e. field 8 of the B 0: line increments from 00 to 06 then repeats; with Date:, B 0: (except field 8 will be 10 (its 10 in the above input file) or 11) and B 120: lines changing).

So, I would like to find the B 0: line that contains 10 or 11 in field 7 and 00 in field 8 and print the line directly before it (the Date: line) and then print that B 0: line it found. Next, I would like awk to find the B 0: line that contains 10 or 11 in field 7 and 04 in field 8 and print the line directly before it (the Date: line), then print the found B 0: line, then print the line directly after the B 0: line (the B 120: line). Finally, I would like awk to find the found B 0: line that contains 10 or 11 in field 7 and 06 in field 8 and print the line directly before it (the Date: line), then print the found B 0: line, then print the line directly after the found B 0: line (the B 120: line). I would like this repeated for every block of data in the order described (again my input file in the example above only covers one blocks worth).

Ideally, I would also like the following to also occur...but if someone doesn't know how to do it..then I would be more than greatful to have the awk line needed to get the previous paragraph done. From time to time parts of data are missing. Missing data will consist always of 3 lines (Date:, B 0: and B 120: lines). So, its possible that one or more of the search lines may be missing (line B 0: with 10 or 11 in field 7 and 00 in field 8, and/or line B 0: with 10 or 11 in field 7 and 04 in field 8, and or line B 0: with 10 or 11 in field 7 and 06 in field 8). If this is the case I would like awk not to print anything and move on to the next block of data (find the next line b 0: with 10 or 11 in field 7 and 00 in field 8) and begin search and print again.

What I would like for an output file (this pattern would repeat if the input file had more than one block of data):

Date: 10-JUN-12 12:00:00
B 0: 00 00 00 00 10 00 16 28
Date: 10-JUN-12 12:00:40
B 0: 00 00 00 00 10 04 11 22
B 120: 00 00 12 87 10 01 13 42
Date: 10-JUN-12 12:01:00
B 0: 00 00 00 00 10 06 11 22
B 120: 00 00 12 87 10 01 13 42

Hopefully this isn't too confusing....I've try quite a few things and haven't had any luck.

:confused:

use some code tags... Be clear in whatever you try to explain us :frowning:

How about this:

awk '$1" "$2=="B 0:"&&$7~"^(10|11)$"&&$8==w {
print p;print; if(w>2) {getline;print};w=w<8?w+2:0; if(w==2)w=4} {p=$0}' infile
1 Like

Thanks for the info...I'll give it a try tomorrow...could you explain the code?

p stores previous line
w cycles thru 0, 4 and 6
getline;print - fetches next line from infile and prints it.
$1" "$2=="B 0:" field 1 is "B" and field 2 is "0:"

print p;print print previous (stored line) and current line.

1 Like

Great...thanks for the help. Sorry for the confusion...but I just realized my data actually goes from b 0: field 8 being 00 to 1F. So, I need w to cycle thru 0 (no change to code), 0F (instead of 4) and 1F (instead of 6). Just to be clear 00 to 1F is in hex so (00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0A, 0B, 0C, 0D, 0E, 0F, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1A, 1B, 1C, 1D, 1E, 1F). How would you change the code for that? Thanks again.