Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Egrep patterns in a file and limit number of matches to print for each pattern match

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-10-2017
ananan ananan is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 12 July 2017, 2:38 AM EDT
Posts: 22
Thanks: 5
Thanked 0 Times in 0 Posts
Egrep patterns in a file and limit number of matches to print for each pattern match

Hi

I need to egrep patterns in a file and limit number of matches to print for each matched pattern.

-m10 option is not working out in my sun solaris 5.10

Please guide me the options to achieve.

if i do head -10 , i wont be getting all pattern match results as output since for a pattern multiple lines will be getting.
Sponsored Links
    #2  
Old Unix and Linux 06-10-2017
Aia's Unix or Linux Image
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 2 August 2017, 1:08 AM EDT
Posts: 1,633
Thanks: 46
Thanked 623 Times in 584 Posts
Got Perl?


Code:
perl -nle 'if(/REGEX/){print; $m++; close ARGV if $m>=10}' example

Sponsored Links
    #3  
Old Unix and Linux 06-10-2017
drl's Unix or Linux Image
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 22 August 2017, 7:51 AM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,149
Thanks: 212
Thanked 396 Times in 343 Posts
Hi, ananan.

Welcome to the forum.

Code:
To get the best advice on transforming, extracting, manipulating data,
please supply your computing environment and problem context:

1. OS and shell, preferably with versions
2. Representative sample input
3. Output desired that corresponds to the input
4. Logic to obtain output from input
5. Attempts at a solution that you have tried, including output

Post code and data inside CODE tags.

If you need to use or to avoid certain tools, you may need
to explain why, especially if your post count is low. Once the
problem is identified, responders often can choose the most
appropriate tool, not necessarily the one you might want.

These guidelines allow responders to create solutions without
ambiguity and to avoid creating sample data sets.

Do you desire a different count for each different pattern?

What should happen if more than one pattern match occurs on the same line?

What should happen if one pattern matches several times on one line?

Is the file short enough so that egrep could be run a number of times on the file?

Best wishes ... cheers, drl
    #4  
Old Unix and Linux 06-10-2017
ananan ananan is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 12 July 2017, 2:38 AM EDT
Posts: 22
Thanks: 5
Thanked 0 Times in 0 Posts
Hi,

SAy the sample file is (actual file size is big 15 MB)

Quote:
cat file.txt
abc1
bcd1
abc2
abc3
abc4
bcd2
bcd3
cde1
cde2
bcd4
cde3
and i am doing an egrep for the patterns abc, bcd, cde and i need to print the matching lines just three times per pattern.

Code:
egrep -i "abc|bcd|cde" file.txt

output should be limited to
Quote:
abc1
abc2
abc3
bcd1
bcd2
bcd3
cde1
cde2
cde3
Even though multiple lines matching the pattern i want only three matching lines per pattern from the entire file(Size is bigger).

If it can be achieved through awk pattern search , is also ok. Like having the patterns in a file and reading that file in a loop and using awk to limit the number of lines to print for each pattern.

i have used

Code:
egrep -i "abc|bcd|cde" file.txt | head -10

but this will limit the output of other patterns if first pattern itself came first for 10 times in my source file.
Sponsored Links
    #5  
Old Unix and Linux 06-10-2017
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 August 2017, 11:08 AM EDT
Location: Aachen, Germany
Posts: 11,189
Thanks: 287
Thanked 3,451 Times in 3,179 Posts
How about

Code:
awk 'match ($0, PAT) && ++T[substr($0, RSTART, RLENGTH)]<4' PAT="abc|bcd|cde" file2
abc1
bcd1
abc2
abc3
bcd2
bcd3
cde1
cde2
cde3

The Following User Says Thank You to RudiC For This Useful Post:
ananan (06-10-2017)
Sponsored Links
    #6  
Old Unix and Linux 06-10-2017
ananan ananan is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 12 July 2017, 2:38 AM EDT
Posts: 22
Thanks: 5
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by RudiC View Post
How about

Code:
awk 'match ($0, PAT) && ++T[substr($0, RSTART, RLENGTH)]<4' PAT="abc|bcd|cde" file2
abc1
bcd1
abc2
abc3
bcd2
bcd3
cde1
cde2
cde3

I am getting error.

Code:
bash-3.00$ awk 'match ($0, PAT) && ++T[substr($0, RSTART, RLENGTH)]<4' PAT="abc|bcd|cde" file2.txt
awk: syntax error near line 1
awk: bailing out near line 1

but works with nawk

Last edited by ananan; 06-10-2017 at 01:39 PM..
Sponsored Links
    #7  
Old Unix and Linux 06-10-2017
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 August 2017, 11:08 AM EDT
Location: Aachen, Germany
Posts: 11,189
Thanks: 287
Thanked 3,451 Times in 3,179 Posts
Quote:
Originally Posted by Don Cragun View Post
If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk or nawk instead of awk.
The Following User Says Thank You to RudiC For This Useful Post:
ananan (06-10-2017)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to print line if two lines above it matches patterns.? Juha Shell Programming and Scripting 3 10-22-2015 03:39 AM
Match pattern and print the line number of occurence using awk redse171 Shell Programming and Scripting 3 06-04-2014 04:03 PM
grep - match files containing minimum number of pattern matches stumpyuk Shell Programming and Scripting 3 01-12-2012 11:45 PM
Match multiple patterns in a file and then print their respective next line AshwaniSharma09 Shell Programming and Scripting 3 04-12-2011 08:26 AM
Sed to delete exactly match pattern and print them in other file new_buddy Shell Programming and Scripting 3 07-10-2008 02:59 AM



All times are GMT -4. The time now is 11:09 AM.