Print lines between the search pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Print lines between the search pattern
# 8  
Old 11-24-2008
Error

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
# 9  
Old 11-24-2008
Quote:
Originally Posted by prsshini
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.
# 10  
Old 11-24-2008
Still not able to

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
# 11  
Old 11-24-2008
You have a typo in the code, remove the slash before the p:

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

# 12  
Old 11-24-2008
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.
# 13  
Old 11-24-2008
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
# 14  
Old 11-24-2008
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

3. Shell Programming and Scripting

Search Pattern and Print lines in Single Column

Hi Experts I have small query where I request the into a single file Suppose: File1: {Unique entries} AA BB CC DD FileB: AA, 123 AA, 234 AA, 2345 CC, 123 CC, 5678 DD,123 BB, 7890 (5 Replies)
Discussion started by: navkanwal
5 Replies

4. Shell Programming and Scripting

Search pattern on logfile and search for day/dates and skip duplicate lines if any

Hi, I've written a script to search for an Oracle ORA- error on a log file, print that line and the .trc file associated with it as well as the dateline of when I assumed the error occured. In most it is the first dateline previous to the error. Unfortunately, this is not a fool proof script.... (2 Replies)
Discussion started by: newbie_01
2 Replies

5. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

6. Shell Programming and Scripting

Print a pattern between the xml tags based on a search pattern

Hi all, I am trying to extract the values ( text between the xml tags) based on the Order Number. here is the sample input <?xml version="1.0" encoding="UTF-8"?> <NJCustomer> <Header> <MessageIdentifier>Y504173382</MessageIdentifier> ... (13 Replies)
Discussion started by: oky
13 Replies

7. Shell Programming and Scripting

Search for Pattern and Print including Lines in between

Gurus, I have a big file that needs to be sorted out and I cant figure out what to do. The file name is as below: Name: xxxx yyyy nnnn Description: dfffgs sdgsgsf hsfhhs afgghhjdgj fjklllll gsfhfh Updated: jafgadsgg gsg Corrected: date today The file consists of line like these. ... (13 Replies)
Discussion started by: The One
13 Replies

8. Shell Programming and Scripting

Search for a pattern in a file and print previous lines from a particular point

Hi, I am new to ksh scripting and I have a problem. I have a file in which I have to search for a particular pattern say 'a' then from that line I need to search for another pattern say 'b' in the previous lines and thne print the file from pattern 'b' till the end of file. For eg: ... (2 Replies)
Discussion started by: umaislearning
2 Replies

9. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question