'/SUN> read db @cmpd unit 60/ {c = 3; getline; s = $0 }
If the current record matches the pattern "SUN> read db @cmpd unit 60",
set the parameter c to 3, read the next record and save it in the parameter s.
!c-- && /tabint>ERROR:/ { print s }
If the value of the parameter c is not true (is zero) and the current record matches the pattern "tabint>ERROR:",print s.
If the expression !c-- is not clear,
consider this:
% cat file
Line 1 SUN> read db @cmpd unit 60
Line 2 Parameter: CMPD -> "C00071"
Line 3
Line 4 SUN> generate
Line 5 tabint>ERROR: (Variable data)
% awk '/SUN> read db @cmpd unit 60/ {
print "c is:", c, "$0 is:", $0; c = 3; getline; s = $0 }
{ print "c is:", c, "$0 is:", $0; c-- }' file
c is: $0 is: Line 1 SUN> read db @cmpd unit 60
c is: 3 $0 is: Line 2 Parameter: CMPD -> "C00071"
c is: 2 $0 is: Line 3
c is: 1 $0 is: Line 4 SUN> generate
c is: 0 $0 is: Line 5 tabint>ERROR: (Variable data)
In this case:
!c-- && /tabint>ERROR:/
The first expression (left side of the logical AND) is evaluated for every record (so c is decremented).
I have a variant of the same question. I have been searching across number of threads, but it has not been covered, apparently.
Any input and explanation of the same would be helpful.
Sample Input
-------------
Record 1
Field 1: Data for field 1
Field 2: Data for field 2
Field 3: Data for field 3
Field 4: Data for field 4
Record 2
Field 1: Data for field 1
Field 2: Data for field 2
Field 4: Data for field 4
Record 3
Field 1: Data for field 1
Field 3: Data for field 3
Field 4: Data for field 4
---------------------------
Expected Output (The new inserts are in CAPS)
---------------------------
Record 1
Field 1: Data for field 1
Field 2: Data for field 2
Field 3: Data for field 3
Field 4: Data for field 4
Record 2
Field 1: Data for field 1
Field 2: Data for field 2
FIELD 3: <NULL>
Field 4: Data for field 4
Record 3
Field 1: Data for field 1
FIELD 2: <NULL>
Field 3: Data for field 3
Field 4: Data for field 4
Logic:
If (^Field) && ( (next occurence of ^Field) <> condition)
insert line as above
endif
endif