Print lines between the search pattern

hi,
I have a file say x.txt containing

xxx
123
bla
bla
...
you
xxx
dfk
dbf
...
me
xxx
...
...
keeps on..

i need to search for pattern in the line starting xxx in the file. If pattern matched, I need to fetch all the lines till i find next xxx.

Please help.

sed -n '/xxx/,/xxx/p' <tst.txt

Will find from the first xxx to the second,
from the third to the fourth,
etc...

Awesome.. Works like exactly what i need.. Thanks a lot... Keep rocking!!!

Hi, one more help pls... can u let me know to print the same lines but just before the line it finds xxx...

Assumimg you want the lines before the first xxx:

awk '/xxx/{exit}{print}' file

Regards

Hi
I m sorry for not being clear.. Its like
I want line from XXX till the line before next xxx..
say if
xxx
1
2
3
4
xxx,
i want the lines from first xxx till line4.
Could you please help. Thanks a lot.

Try this:

awk 'p && /xxx/{exit} /xxx/{p=1}p' file

Hi
I am sorry but it gives the below error.
$ awk 'p && /xxx/{exit} /xxx/{p=1}p' test.datx
awk: syntax error near line 1
awk: bailing out near line 1

Please help. Thanks

Use nawk or /usr/xpg4/bin/awk on Solaris.

Hi, Sorry to bother you. I am still not able to make it.. It says as follows..

$ uname -a
SunOS wdcsn99a 5.9 Generic_122300-28 sun4u sparc SUNW,Sun-Fire-15000
$ nawk 'p && /xxx/{exit} /xxx/{p=1}/p' test
nawk: syntax error at source line 1
context is
p && /xxx/{exit} >>> /xxx/{p=1}/p <<<
nawk: bailing out at source line 1
$ /usr/xpg4/bin/awk 'p && /xxx/{exit} /xxx/{p=1}/p' test
/usr/xpg4/bin/awk: EOF in regular expression
Context is:
>>> p && /xxx/{exit} /xxx/{p=1}/p <<<
$

Please help. Thanks

You have a typo in the code, remove the slash before the p:

 'p && /xxx/{exit} /xxx/{p=1}/p' test

Hi, Thanks a lot for your patience. But this is giving me only the first occurance in the file. I want to search the file till EOF and print the lines between search patterns. For Eg;
xxx1
1
2
3
xxx2
4
5
6
xxx1
7
8
9
xxx2
10
11
...

and i want the output as,
xxx1
1
2
3
xxx1
7
8
9

I used
nawk "p && /xxx1/{exit} /xxx/{p=1}p" $file

and it gives me only first occurance. Could you please clarify. Thanks again.

Please be accurator, do you want the sections between xxx1 and xxx2 or every occurance after the 1st xxx till the 2nd xxx, the 3th xx till the 4th etc?
It's better to post your original file and the desired output.

Regards

Okay. Here is the original file.I would want to search for the pattern CID42H5ZNBWC119C1094445.
When I find the search pattern, i want to print the lines till I find the next CID. This needs to be searched in the whole file and the results need to be printed in a file. Hope it is clear. Thanks a lot for your patience.

INPUT FILE:

CID42H5ZNBWC119C1094445 WCMP
PATSTEVEN PRIOR
EMPCONCRETE CONSTRUCTION NOC - IN300.00W
EVT20070124WALKING UP STAIRS AND Tparticipati STRAIN OF THE RIGHT ANKLE 5549
CLMLOSTCT WCMPDENNOT20070228200C446300 061016108
SVR
APLACE_ELECTRONIC_FLAG *A
APLACE_D_VALUES UX
APLACE_OTHER_VALUES INDN566O
CIM CDAC
CID42H5ZNBWC119C1592118
PATCHRISTOPHE COWAN TPKE
EMPPLUMBING NOC
EVT20070129WHILE WALKING SLIPPED AWCHCN RIGHT FOOT FRACTURE 5549
CLMLOSTCT WCMPDENNOT20070228200C446304 061016108
SVR
APLACE_ELECTRONIC_FLAG *A
APLACE_D_VALUES UX
APLACE_OTHER_VALUES INDN566O N
CIM CDAC
CID42H5ZNBWC119C1094445 W
PATVALERIE GOODWIN
EMPHOSPITAL - PROFESSIONAL EMPLOYEE352.99W
EVT20020729EE WAS TRANSFERING PATIEclaim CONTUSION OF THE LEFT ELBOW/TENDO3210
CLMLOSTGA WCMPDENNOT20070228200C429910Y 951077060
SVR
APLACE_ELECTRONIC_FLAG *A
APLACE_D_VALUES FI
APLACE_OTHER_VALUES PEIN788OFI O CORPORATION
CIM
CID1233863487368987394829

OUTPUT FILE MUST BE LIKE BELOW:

CID42H5ZNBWC119C1094445 WCMP
PATSTEVEN PRIOR
EMPCONCRETE CONSTRUCTION NOC - IN300.00W
EVT20070124WALKING UP STAIRS AND Tparticipati STRAIN OF THE RIGHT ANKLE 5549
CLMLOSTCT WCMPDENNOT20070228200C446300 061016108
SVR
APLACE_ELECTRONIC_FLAG *A
APLACE_D_VALUES UX
APLACE_OTHER_VALUES INDN566O
CIM CDAC
CID42H5ZNBWC119C1094445 W
PATVALERIE GOODWIN
EMPHOSPITAL - PROFESSIONAL EMPLOYEE352.99W
EVT20020729EE WAS TRANSFERING PATIEclaim CONTUSION OF THE LEFT ELBOW/TENDO3210
CLMLOSTGA WCMPDENNOT20070228200C429910Y 951077060
SVR
APLACE_ELECTRONIC_FLAG *A
APLACE_D_VALUES FI
APLACE_OTHER_VALUES PEIN788OFI O CORPORATION
CIM

This should give the desired output:

nawk 'p && /^CID/{p=0}/CID42H5ZNBWC119C1094445/{p=1}p' file

GREAAAAAAAATTTTTTTTtt.... WORKED PERFECT!!!!!!! THAAAAAAAANKKKK YOUUUUUUUUUU... Could you tell me how do u manage to build these commands.. I also read manual but I am not able to do it at this level.. Is there anything else that i should read to get this level of knowledge. Please guide. Thank you again a lot for your patience and rendering me this help. Good Luck and please guide me to improve my Unix knowledge. Thank you.

There are a lot of manuals and tutorials on the net. It's a matter of reading and experiencing it yourself.
Try to solve the problems on this forum you're interesting in (search for awk) and compare your solution with the others.

Regards

Sure.... Thanks Frank.. Nice working with you. Good Luck, Regards.