Search for sequential pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for sequential pattern
# 8  
Old 09-23-2016
How about
Code:
awk  -v PAT="two,three,four" '                  # define search pattern
BEGIN   {KC = split(PAT, T, ",")                # fill temp array with keywords from pattern, find key count
         RB = KC + 2                            # compute ring buffer size
        }
        {A[NR%RB] = $1                          # fill ring buffer for line number
         B[NR%RB] = $2                          # fill ring buffer for keywords
         if ($2 == T[CNT+1])    CNT++           # if keyword match found: advance to next keyword
         else                   CNT = 0         # else start again from first keyword

         if (CNT == KC) {printf "%s,", A[(NR+1)%RB]
                                                # BINGO! ALL keywords in a row! print line number

                         for (i=1; i<=2; i++) printf "%s,", B[(NR+i)%RB]
                                                # print two preceding words

                         print PAT              # print the search words 
                         CNT = 0                # and start again from first keyword
                        }
        }
' file
3,horse,one,two,three,four

# 9  
Old 09-23-2016
And, if you want a dynamic number of preceding lines, try
Code:
awk  -v PRC=4 -v PAT="two,three,four" '         # define search pattern
BEGIN   {KC = split(PAT, T, ",")                # fill temp array with keywords from pattern, find key count
         RB = KC + PRC                          # compute ring buffer size
        }
        {A[NR%RB] = $1                          # fill ring buffer for line number
         B[NR%RB] = $2                          # fill ring buffer for keywords
         if ($2 == T[CNT+1])    CNT++           # if keyword match found: advance to next keyword
         else                   CNT = 0         # else start again from first keyword

         if (CNT == KC) {printf "%s,", A[(NR+1)%RB]
                                                # BINGO! ALL keywords in a row! print line number, -

                         for (i=1; i<=PRC; i++) printf "%s,", B[(NR+i)%RB]
                                                # print two preceding words

                         print PAT              # print the search words 
                         CNT = 0                # and start again from first keyword
                        }
        }
' file

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

Extracting sequential pattern

Hi, Can someone advise/help me on how to write a script to extract sequential lines. I was able to find and get a script working to create permutations of the inputs, but that not what I want/need. awk 'function perm(p,s, i) { for(i=1;i<=n;i++) if(p==1) ... (4 Replies)
Discussion started by: fuzzi
4 Replies

3. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

4. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 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. Programming

Tool to simulate non-sequential disk I/O (simulate db file sequential read) in C POSIX

Writing a Tool to simulate non-sequential disk I/O (simulate db file sequential read) in C POSIX I have over the years come across the same issue a couple of times, and it normally is that the read speed on SAN is absolutely atrocious when doing non-sequential I/O to the disks. Problem being of... (7 Replies)
Discussion started by: vrghost
7 Replies

7. 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

8. Shell Programming and Scripting

Append specific lines to a previous line based on sequential search criteria

I'll try explain this as best I can. Let me know if it is not clear. I have large text files that contain data as such: 143593502 09-08-20 09:02:13 xxxxxxxxxxx xxxxxxxxxxx 09-08-20 09:02:11 N line 1 test line 2 test line 3 test 143593503 09-08-20 09:02:13... (3 Replies)
Discussion started by: jesse
3 Replies

9. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

10. Programming

Reading special characters while converting sequential file to line sequential

We have to convert a sequential file to a 80 char line sequential file (HP UX platform).The sequential file contains special characters. which after conversion of the file to line sequential are getting coverted into "new line" or "tab" and file is getting distorted. Is there any way to read these... (2 Replies)
Discussion started by: Rajeshsu
2 Replies
Login or Register to Ask a Question