Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search for pattern and print lines only if pattern found in more than 1 line

Status
Not open for further replies.

marksand

Technical User
Nov 8, 2015
1
0
0
GB
hi,
I'd like to check if pattern occurs in multiple lines in a file and print those lines (if pattern found only once then ignore and don't print the line).
regards, Mark.
 
Hi

The simplest is to collect the matching lines and output them only ad the end if the count condition is met :
Code:
[fuchsia]/pattern/[/fuchsia][teal]{[/teal]m[teal][++[/teal]n[teal]]=[/teal][navy]$0[/navy][teal]}[/teal][b]END[/b][teal]{[/teal][b]if[/b][teal]([/teal]n[teal]>[/teal][purple]1[/purple][teal])[/teal][b]for[/b][teal]([/teal][navy]i[/navy][teal]=[/teal][purple]1[/purple][teal];[/teal]i[teal]<=[/teal]n[teal];[/teal]i[teal]++)[/teal][b]print[/b] m[teal][[/teal]i[teal]]}[/teal]

If need to process huge amount of data, then better rewrite it to delay outputting the first match only until the second match is found.

Tested with [tt]gawk[/tt] and [tt]mawk[/tt].


Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top