Printing paragraph between semicolons having XXXX

Hello experts,

I have below file:

1
2
3
4
5
6
;
7
8
9
10
XXXX
1
;
2
3
4
5
6
;
7
8
XXXX
9
;
10
1
2
3
4
;
5
XXXX
6
7
;
8
9
10
1
2
3
4
5
;
YYYY
6
7
;
8
9
10

I would like to print the paragraph between semicolons having XXXX (not YYYY). The idea is to match a pattern XXXX and print its respective segment which is enclosed in semicolons. So the required output of the above file will be as follows:

;
7
8
9
10
XXXX
1
;
;
7
8
XXXX
9
;
;
5
XXXX
6
7
;

Anything in sed/awk/perl would be a great idea.

Thanks ,
Manu

Hello manuswami,

Kindly use the code tags while posting commands and codes. Followng may help you in same.

awk '($0 ~ /;/){set++;} set{v=v?v ORS $0:$0} (v ~ /XXXX/){value=1} {if(set>1 && value==1){print v;v="";value="";set=""}}'  filename

Output will be as follows.

;
7
8
9
10
XXXX
1
;
;
7
8
XXXX
9
;
;
5
XXXX
6
7
;

Thanks,
R. Singh

Try

$ awk 'function dothis(){ if(v && s==2){ print p} if(s==2)s=v=p="" }{ dothis(); s += /;/ ; v += /XXXX/; s ? p = length(p) ? p ORS $0 : $0 : "" }END{dothis()}' file

Try (untested)

awk '!/XXXX/ {$0=""}1 ' RS=";" ORS";" file
awk 'NR>1{if(/XXXX/) print RS $0} END{print ";\n"}' RS=\; ORS= file