I have a combination of patterns to search.
file.txt contains below:
H2016-02-10
A74867712
I1556539758
Xjdflk534jfl
W0000055722327732
W0000056029009389
A74867865
I1556536434
W0000055822970840
W0000055722325916
A74868015
I1556541270
C0000055928920421
E
lines starting with A are start of a recordset till we reach the next line starting with A. so in this file, i have 3 recordsets.
now from each recordset, I want to print first 2 lines, if there is no line starting with X in the recordset.
for example: from this file, i need to print:
A74867865
I1556536434
but i dont need to print:
A74868015
I1556541270
because there is no line starting with W in this recordset.
Do not expect us to read your mind as your post is not making any sense to me so state what you need clearly...
my requirement is:
file.txt can have lines starting with either A,I,X,W,C,P, ignoring header line starting with H at the beginning of the file
and footer line starting with E at the end of file.
lines starting with A are start of a recordset till we reach the next line starting with A.
in the example i have given, tehre are 3 recordsets.
A74867712
I1556539758
Xjdflk534jfl
W0000055722327732
W0000056029009389
A74867865
I1556536434
W0000055822970840
W0000055722325916
A74868015
I1556541270
C0000055928920421
I want to print first 2 lines of those recordsets, for which there is no X line, whenever there is a W line.
so in the above 3 recordsets i need to print only the 2nd one:
i.e.
A74867865
I1556536434
W0000055822970840
W0000055722325916
i am trying with awk.
RudiC
March 8, 2016, 1:08pm
4
Please use code tags as required by forum rules!
How about
awk '
(/^A/ || /^E/) &&
W && !X {print A
print B
}
/^A/ {W = X = 0
A = $0
getline B
}
/^W/ {W = 1
}
/^X/ {X = 1
}
' file
A74867865
I1556536434
Another way, first putting empty lines between records:
awk '/^[AHE]/{print x}1' file | awk '/\nW/ && !/\nX/{print $1 ORS $2}' FS='\n' RS=
@RudiC :
Thanks for that it works perfectly.