1

Have text like below, need print only the block that contains ABC:CDA* and fgh:qwe.

Tried below Awk but its print all data.

awk -n  -v RS='' -v ORS='\n\n' 'match($0,".*fgh:qwe[^\n]*") { print substr($0,1,RLENGTH) }'

Input:

ABC:CDA_sd
dfg:GFJ
dfg:jasdn
iop:yui
ghj:bhu
ghj:poi
fgh:qwe

axy:ABC_gf
dfg:GFJ
dfg:jasdn
iop:yui
ghj:bhu
ghj:poi
fgh:qwe

ABC:CDA_ghg
dfg:GFJ
dfg:jasdn
iop:yui
ghj:bhu
ghj:poi
fgh:qwe

ABC:CDA_jsagdh
dfg:GFJ
dfg:jasdn
iop:yui
ghj:bhu
ghj:poi
fgh:qwe

Output:

ABC:CDA_sd
dfg:GFJ
dfg:jasdn
iop:yui
ghj:bhu
ghj:poi
fgh:qwe

ABC:CDA_ghg
dfg:GFJ
dfg:jasdn
iop:yui
ghj:bhu
ghj:poi
fgh:qwe

ABC:CDA_jsagdh
dfg:GFJ
dfg:jasdn
iop:yui
ghj:bhu
ghj:poi
fgh:qwe
1

In order to have more control on the output format, I suggest Perl in paragraph mode

perl -00nE 'say "$1" if /ABC:CDA(.*)fgh:qwe/s' example

Perl -00ne means: for each paragraph

1

try:

awk -v RS= '/^ABC:CDA.*fgh:qwe$/{ print sep $0; sep=ORS }' infile
0
0

Tested in GNU awk.

awk '/^(axy:ABC_|ABC:CDA_)[[:lower:]]{2,6}$/ {check = ($1 == "axy:ABC_gf")} !check' file
ABC:CDA_sd
dfg:GFJ
dfg:jasdn
iop:yui
ghj:bhu
ghj:poi
fgh:qwe

ABC:CDA_ghg
dfg:GFJ
dfg:jasdn
iop:yui
ghj:bhu
ghj:poi
fgh:qwe

ABC:CDA_jsagdh
dfg:GFJ
dfg:jasdn
iop:yui
ghj:bhu
ghj:poi
fgh:qwe

Or only that data:


awk '/^(axy:ABC_|ABC:CDA_)[[:lower:]]{2,6}$/ {check = ($1 == "axy:ABC_gf")} check' file
axy:ABC_gf
dfg:GFJ
dfg:jasdn
iop:yui
ghj:bhu
ghj:poi
fgh:qwe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.