Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Finding the same pattern in three consecutive lines in several files in a directory


 
Thread Tools Search this Thread
# 1  
Finding the same pattern in three consecutive lines in several files in a directory

I know how to search for a pattern/regular expression in many files that I have in a directory. For example, by doing this:
Code:
grep -Ril "News/U.S." .

I can find which files contain the pattern "News/U.S." in a directory.

I am unable to accomplish about how to extend this code so that it can search for 3 consecutive lines in the files which contain the above pattern. The code should only output file names which contain three consecutive times the above pattern, and disregard less than 2 or 4 or greater.

I mean:
Code:
News/U.S.
News/U.S.
News/U.S.

As you can see News/U.S. appears 3 times consecutively in newlines. I want to output those file names which contain the above pattern. I am using Linux with BASH.
# 2  
I don't think you can do this with grep. You may want to try this awk that works with the sample you posted. For different input it may need to be modified.
Code:
awk '/News\/U.S./ {CNT++; next} CNT==3 {print FILENAME} {CNT=0}' file

This User Gave Thanks to RudiC For This Post:
 

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern
father_7
I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00...... Shell Programming and Scripting
9
Shell Programming and Scripting
find pattern matches in consecutive lines in certain fields-awk
jvoot
I have a text file with many thousands of lines, a small sample of which looks like this: InputFile:PS002,003 D -1 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 6 -1 -1 -1 -1 0 509 0 PS002,003 PSQ 0 1 7 18 1 0 -1 1 1 3 -1 -1 ...... UNIX for Beginners Questions & Answers
5
UNIX for Beginners Questions & Answers
Eliminate consecutive lines with the same pattern
sarn_nat
Hi, I would like to know how to remove lines which has the same pattern as the next line through sed/awk. Stream 39 (wan stream 7) Stream 40 (wan stream 8) WINQ Counter 115955 1 1613 (BYTE) 11204787 163 ...... UNIX for Dummies Questions & Answers
2
UNIX for Dummies Questions & Answers
How to insert line with between two consecutive lines that match special pattern?
jjnight
I have following pattern in a file: 00:01:38 UTC abcd 00:01:48 UTC 00:01:58 UTC efgh 00:02:08 UTC 00:02:18 UTC and I need to change something like the following 00:01:38 UTC abcd 00:01:48 UTC XXXX 00:01:58 UTC efgh 00:02:08 UTC XXXX... Shell Programming and Scripting
6
Shell Programming and Scripting
merging of 2 consecutive lines in a file for a specific pattern
novice_man
Hi , I'm looking for a way to merge two lines only for a given pattern / condition. Input : abcd/dad + -49.201 2.09 -49.5 34 ewrew rewtre * fdsgfds/dsgf/sdfdsfasdd + -4.30 0.62 -49.5 45 sdfdsf cvbbv * sdfds/retret/asdsaddsa + ...... Shell Programming and Scripting
1
Shell Programming and Scripting

Featured Tech Videos