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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Egrep patterns in a file and limit number of matches to print for each pattern match
# 1  
Old 06-10-2017
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.
# 2  
Old 06-10-2017
Got Perl?

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

# 3  
Old 06-10-2017
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 06-10-2017
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.
# 5  
Old 06-10-2017
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

This User Gave Thanks to RudiC For This Post:
# 6  
Old 06-10-2017
Quote:
Originally Posted by RudiC
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 02:39 PM..
# 7  
Old 06-10-2017
Quote:
Originally Posted by Don Cragun
If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk or nawk instead of awk.
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. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

How to print line if two lines above it matches patterns.?

Hi, I could only find examples to print line before/after a match, but I'd need to print line after two separate lines matching. E.g.: From the below log entry, I would need to print out the 1234. This is from a huge log file, that has a lot of entries with "CLIENT" and "No" entries (+ other... (3 Replies)
Discussion started by: Juha
3 Replies

4. Shell Programming and Scripting

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

5. Shell Programming and Scripting

Match 2 different patterns and print the lines

Hi, i have been trying to extract multiple lines based on two different patterns as below:- file1 @jkm|kdo|aas012|192.2.3.1 blablbalablablkabblablabla sjfdsakfjladfjefhaghfagfkafagkjsghfalhfk fhajkhfadjkhfalhflaffajkgfajkghfajkhgfkf jahfjkhflkhalfdhfwearhahfl @jkm|sdf|wud08q|168.2.1.3... (8 Replies)
Discussion started by: redse171
8 Replies

6. Shell Programming and Scripting

grep - match files containing minimum number of pattern matches

I want to search a bunch of files and list only those containing a minimum number of pattern matches. So if I want to identify files containing 3 (or more) instances of the pattern "said:" and I have file1 that contains the lines: He said: She said: and file2 that contains the lines: He... (3 Replies)
Discussion started by: stumpyuk
3 Replies

7. UNIX for Dummies Questions & Answers

extracting lates pattern match from multiple matches in log

Hi, I have a large, multiline log file. I have used pcregrep to extract all entries in that log that match a particular pattern - where that pattern spans multiple lines. However, because the log file is large, and these entries occur every few minutes, I still output a very large amount... (6 Replies)
Discussion started by: dbrb2
6 Replies

8. Shell Programming and Scripting

Match multiple patterns in a file and then print their respective next line

Dear all, I need to search multiple patterns and then I need to print their respective next lines. For an example, in the below table, I will look for 3 different patterns : 1) # ATC_Codes: 2) # Generic_Name: 3) # Drug_Target_1_Gene_Name: #BEGIN_DRUGCARD DB00001 # AHFS_Codes:... (3 Replies)
Discussion started by: AshwaniSharma09
3 Replies

9. Shell Programming and Scripting

print lines which match multiple patterns

Hi, I have a text file as follows: 11:38:11.054 run1_rdseq avg_2-5 999988.0000 1024.0000 11:50:52.053 run3_rdrand 999988.0000 1135.0 128.0417 11:53:18.050 run4_wrrand avg_2-5 999988.0000 8180.5833 11:55:42.051 run4_wrrand avg_2-5 999988.0000 213.8333 11:55:06.053... (2 Replies)
Discussion started by: annazpereira
2 Replies

10. Shell Programming and Scripting

Sed to delete exactly match pattern and print them in other file

Hi there, I need help about using sed. Iam using sed to delete and print lines that match the port number as listed in sedfile. I am using -d and -p command for delete match port and print them respectively. However, the output is not synchonize where the total deleted lines is not similar with... (3 Replies)
Discussion started by: new_buddy
3 Replies
Login or Register to Ask a Question