grep a specific amount of occurrence


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers grep a specific amount of occurrence
# 1  
Old 07-08-2012
Java grep a specific amount of occurrence

hey , i m trying to figure out how to do the following :

i got a text file the looks like so:

Code:
 
1031
1031
1031
1031
1031
1031
1031
1031
16500
16500
16500
16500
1031
1031
1031
1031
1031
16500
16500
16500

i want to grep a specific amount of a value , for example
grep of "16500" for 3 occurrences the output will be:

Code:
16500
16500
16500

note that this is a part of a table i dont want to just print the value - X times.

please advise
thanks.
# 2  
Old 07-08-2012
Hi.

For GNU grep:
Code:
       -m NUM, --max-count=NUM
              Stop reading a file after NUM matching lines.
-- excerpt from man grep, q.v.

Best wishes ... cheers, drl
# 3  
Old 07-08-2012
If you do not have GNU grep, you can do it with awk (the specifics depend on what your data looks like).

Regards,
Alister
# 4  
Old 08-01-2012
egrep -i '16500' | wc -l

i think it will do it.
# 5  
Old 08-06-2012
Quote:
Originally Posted by vagar11
egrep -i '16500' | wc -l
This will count the occurrences of "16500" but not limit the result set to a specific number of them. There is the "-c" switch of grep for that, btw..

Code:
grep <regexp> | head -<number>

Will give you the first <number> of hits, while

Code:
grep <regexp> | tail -<number>

will give you the last <number> of hits.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep util last occurrence

I have file contents /tmp/x/abc.txt /home/bin/backup/sys/a.log I need this output: /tmp/x/ /home/bin/backup/sys/ Can somebody please help me out Please use CODE tags as required by forum rules! (3 Replies)
Discussion started by: jhonnyrip
3 Replies

2. Shell Programming and Scripting

Run a command for specific amount of time with an auto key press

Hi, I have been trying to do a small fun project for myself. I want to run a command for 45 seconds. And to get the final output of this command, the script requires I push the "q" key on my keyboard and then the final output file becomes available. I tried the following script. But it... (12 Replies)
Discussion started by: jacobs.smith
12 Replies

3. Shell Programming and Scripting

Adding text to the end of the specific line in a file(only to the first occurrence of it)

Hi, I want to add a text to the end of the specific line in a file. Now my file looks like this: 999 111 222 333 111 444 I want to add the string " 555" to the end of the first line contaning 111. Moreover, I want to insert a newline after this line containg the "000" string. The... (8 Replies)
Discussion started by: wenclu
8 Replies

4. Shell Programming and Scripting

Shifting result of echo by specific amount

I am using echo "HELLO" I want to specify a number shiftWt so that I move hello forward by shiftWt charcaters. Is there a way to do this? (2 Replies)
Discussion started by: kristinu
2 Replies

5. UNIX for Dummies Questions & Answers

Is it possible to grep a certain amount of characters?

I have a file similar to the following filler filler filler car 6 mazda filler filler filler filler car civic honda car rav 4 toyota filler filler If i do a "grep -i car file.txt" the output would be car 6 mazda car civic honda car rav 4 toyota however, i want to have the... (4 Replies)
Discussion started by: jl487
4 Replies

6. UNIX for Dummies Questions & Answers

grep first occurrence but continue to next entry in patternfile

I have 1300 files (SearchFiles0001.txt, SearchFiles0002.txt, etc.) , each with 650,000 lines, tab-delimited data. I have a pattern file, with about 1000 lines with a single word. Each single word is found in the 1300 files once. If I grep -f PatternFile.txt SearchFiles*.txt >OutputFile.txt... (2 Replies)
Discussion started by: newhavendweeb
2 Replies

7. Shell Programming and Scripting

Generate specific amount of same characters

Hi, Is there a command to print one character x amont of times? I need for example 10 comma's (,,,,,,,,,,). Instead of creating a loop, I was wondering if there is a way to do this with sed or awk? Thanks! (3 Replies)
Discussion started by: Subbeh
3 Replies

8. UNIX for Dummies Questions & Answers

Line number of an occurrence using grep

Hi All, is there a way to extract the line number of an occurrence using grep? I know that with the -n option it prints out the line number as well. I would like to assign the line number to a variable. Thanks, Sarah (5 Replies)
Discussion started by: f_o_555
5 Replies

9. UNIX for Dummies Questions & Answers

grep usage - exit after first occurrence

Hi, Is there any way of using grep (this may be done in awk, not sure?) that I can stop grep'n a file once I have found the first occurrence of my search string. Looking through grep man pages -q will exit without printing the lines after the first match, but I need the output. I have... (5 Replies)
Discussion started by: nhatch
5 Replies

10. Shell Programming and Scripting

Grep for the same occurrence or maybe Sed

Hi, I have a file that looks like this dasdjasdjoasjdoasjdoa SYN dakspodkapsdka asdasdasdasdasdasdasd SYN sdfsdfsdfsdfdf shfishifhsdifhsidhfif fsdfsdfsdfsdfs sdfsdfsdfsdsdfsdfsdff cercercercerce sdasdajsdoajsodasodoo FIN dasdaskdpasdda... (4 Replies)
Discussion started by: hcclnoodles
4 Replies
Login or Register to Ask a Question