UNIX help to print 50 lines after every 3rd occurrence pattern till end of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX help to print 50 lines after every 3rd occurrence pattern till end of file
# 1  
Old 10-01-2014
UNIX help to print 50 lines after every 3rd occurrence pattern till end of file

I need help with extract/print lines till stop pattern. This needs to happen after every 3rd occurrence of start pattern and continue till end of file. Consider below is an example of the log file. my start pattern will be every 3rd occurrence of ERROR_FILE_NOT_FOUND and stop pattern will be Stop_pattern
Code:
ERROR_FILE_NOT_FOUND
some textlsjljsl
ERROR_FILE_NOT_FOUND
some textjljss0
ERROR_FILE_NOT_FOUND
some textwwkw
Extract line 1
Extract line 2
Extract line 3
Stop_pattern
some text99mlll
ERROR_FILE_NOT_FOUND
some textjsjsj1
ERROR_FILE_NOT_FOUND
some text9kk
ERROR_FILE_NOT_FOUND
some textllsljlj
Extract line 5
Extract line 6
Extract line 7
Stop_pattern
some textljljljsl
ERROR_FILE_NOT_FOUND
some textppi
ERROR_FILE_NOT_FOUND
some textnnn
ERROR_FILE_NOT_FOUND
some texthkhk
Extract line 9
Extract line 10
Extract line 11
Stop_pattern


desired output:
Code:
Extract line 1
Extract line 2
Extract line 3
//separator if possible
Extract line 5
Extract line 6
Extract line 7
//separator if possible
Extract line 9
Extract line 10
Extract line 11
//separator if possible

---------- Post updated at 02:21 PM ---------- Previous update was at 02:11 PM ----------

ignore the input/output provided in last thread here's correct one however problem statement remains same
Code:
ERROR_FILE_NOT_FOUND
some text
ERROR_FILE_NOT_FOUND
some text
ERROR_FILE_NOT_FOUND
Extract line 1
Extract line 2
Extract line 3
Stop_pattern
some text
ERROR_FILE_NOT_FOUND
some text
ERROR_FILE_NOT_FOUND
some text
ERROR_FILE_NOT_FOUND
Extract line 5
Extract line 6
Extract line 7
Stop_pattern
some text
ERROR_FILE_NOT_FOUND
some text
ERROR_FILE_NOT_FOUND
some text
ERROR_FILE_NOT_FOUND
Extract line 9
Extract line 10
Extract line 11
Stop_pattern

desired output:
Code:
Extract line 1
Extract line 2
Extract line 3
Extract line 5
Extract line 6
Extract line 7
Extract line 9
Extract line 10
Extract line 11


Last edited by Franklin52; 10-01-2014 at 04:53 PM.. Reason: Please use code tags
# 2  
Old 10-01-2014
Try
Code:
awk '/Stop_pattern/ {P=0} P==3; /ERROR_FILE_NOT_FOUND/ {P++}' file
Extract line 1
Extract line 2
Extract line 3
Extract line 5
Extract line 6
Extract line 7
Extract line 9
Extract line 10
Extract line 11

# 3  
Old 10-01-2014
Can you explain command pls, just wanted to make sure my problem statement was understood.

---------- Post updated at 02:39 PM ---------- Previous update was at 02:33 PM ----------

I got this error while running

awk: record ` "Cookie" Str...' too long
record number 16374
# 4  
Old 10-01-2014
Code:
awk     '/Stop_pattern/         {P=0}   # stop pattern will reset the print counter
         P==3                           # if true (i.e. pattern found thrice), do default action = print
         /ERROR_FILE_NOT_FOUND/ {P++}   # count start patterns
        ' file

---------- Post updated at 21:44 ---------- Previous update was at 21:42 ----------

There's no " "Cookie" Str..." " involved.
# 5  
Old 10-01-2014
Thanks for reply.

'/Stop_pattern/ {P=0} # stop pattern will reset the print counter

why are we resetting print counter ?
# 6  
Old 10-01-2014
So P==3 is not true any more, printing stops, and a new cycle with three start patterns can start.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies

2. Emergency UNIX and Linux Support

Read values in each col starting 3rd row.Print occurrence value.

Hello Friends, Hope all are doing fine. Here is a tricky issue. my input file is like this 07 10 14 20 21 03 15 27 30 32 01 10 11 19 30 02 06 14 15 17 01 06 20 25 29 Logic: 1. Please print another column as "0-0-0-0-0" for the first and second rows. 2. Read the first column... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

3. Shell Programming and Scripting

Pattern match till the end of the file.

I have a file which is like this ……………………………………….. ………………………………… ………………………………… …………………………………… ……………………………………. ……………………………… <<<from_here>>> ……………………………… ………………………………. I want a script which would fetch the data starting from <<<from_here>>> in the file till the end... (2 Replies)
Discussion started by: halfafringe
2 Replies

4. Shell Programming and Scripting

sed print from last occurrence match until the end of file

Hi, i have file f1.txt with data like: CHECK a b CHECK c d CHECK e f JOB_START .... I want to match the last occurrence of 'CHECK' until the end of the file. I can use awk: awk '/^CHECK/ { buf = "" } { buf = buf "\n" $0 } END { print buf }' f1.txt | tail +2Is there a cleaner way of... (2 Replies)
Discussion started by: ysrini
2 Replies

5. Shell Programming and Scripting

print only the first occurrence of a pattern

Hi, I have a file as below select or create proc /*comments*/ /*comments*/ /*comments*/ /*comments*/ ( variables4 datatypes1, variables1 datatypes2, variables2 datatypes3, variables3 datatypes2 ) some text some text ( sometext some text ) some text some text (3 Replies)
Discussion started by: manasa_vs
3 Replies

6. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

7. Shell Programming and Scripting

How to print lines till till a pattern is matched in loop

Dear All I have a file like this 112534554 446538656 444695656 225696966 226569744 228787874 113536566 443533535 222564552 115464656 225445345 225533234 I want to cut the file into different parts where the first two columns are '11' . The first two columns will be either... (3 Replies)
Discussion started by: anoopvraj
3 Replies

8. UNIX for Advanced & Expert Users

Urgent Help required : awk/sed help to find pattern and delete till end of line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (1 Reply)
Discussion started by: rajan_san
1 Replies

9. Shell Programming and Scripting

Urgent! Sed/Awk Filter Find Pattern Delete Till End Of Line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (2 Replies)
Discussion started by: rajan_san
2 Replies

10. Shell Programming and Scripting

Print starting 3rd line until end of the file.

Hi, I want to Print starting 3rd line until end of the file. Pls let me know the command. Thanks in advance. (1 Reply)
Discussion started by: smc3
1 Replies
Login or Register to Ask a Question