awk | stop after specified number of results


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk | stop after specified number of results
# 1  
Old 03-15-2004
awk | stop after specified number of results

I am searching some rather large text files using grep and or awk.
What I would like to know is if there is a way (either with grep, awk, or realy any other unix tool) to stop the search when a predifined number of results are returned.

I would like to do this for speed purpuses. When i get 2000 results I would like grep or awk to brake out of the loop and return those results.

This may not be possible with either grep or awk, but any solution that would speed my search in this way or some other, would be very helpfull.
# 2  
Old 03-15-2004
how about "awk '{ print $blah blah blah }'|head -2000"
# 3  
Old 03-15-2004
will awk still run the entire file

Question if I use the "head" function
will that casue the awk to stop looping through the text file when it reaches 2000 result lines or will it simply truncate the results to 2000 after awk has finished.
# 4  
Old 03-15-2004
There is a pipe connecting the two processes. When the head process exits, writes to the pipe are no longer possible. The awk process will recieve a SIGPIPE and abort at the next attempted write.
# 5  
Old 03-16-2004
Or use the exit function in awk to exit immediately, e.g...

awk '/pattern/ {print; count++; if (count=2000) exit}'
This User Gave Thanks to Ygor For This Post:
# 6  
Old 03-16-2004
thanyou

thanks for your help.
bolth solutions work great!
# 7  
Old 03-17-2004
awk '{if (NR <=2000) print blah blah blah }' filename

offcourse this solution is for 2000 lines, so in case if one line has multiple pattern then this may not work for you.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep command to show the number of results

Hi I wanted to know if there is an option in grep command to show the number of results (not the number of lines of findings). Thanks (14 Replies)
Discussion started by: abdossamad2003
14 Replies

2. Shell Programming and Scripting

Attach filename to wc results on massive number of files

Hello, I have massive number of big files that needed to be counted for the total number of lines (> 100x millions) each. I want the file name attached to the count results so that they are aligned nicely matching name and counts. I could do each file at a time, which will take hours to finish,... (8 Replies)
Discussion started by: yifangt
8 Replies

3. Shell Programming and Scripting

How to count number of results found?

Hi guys, I'm struggling with this one, any help is appreciated. I have File1 with hundreds of unique words, like this: word1 word2 word3 I want to count each word from file1 in file2 and return how many times each word is found. I tried something like this: for i in $(cat file1); do... (13 Replies)
Discussion started by: demmel
13 Replies

4. Shell Programming and Scripting

How to pass port number in stop script

In unix i have to start service and it promts to enter port number: /usr/mydir/ca &gt;./stop_ca_devp And it prompts message like : Enter the port number of the server you would like to shut down : Then I type xxxx then it stops the service.Now what I need is remove manual intervention.... (3 Replies)
Discussion started by: krsnadasa
3 Replies

5. Shell Programming and Scripting

Awk - Count instances of a number in col1 and put results in a col2 (new) of diff file

I have 2 files as follows: filename1: : 6742 /welcome/mundial98_ahf1_404.htm 1020 6743 /welcome/mundial98_ahf1_404.htm 2224 6744 /welcome/mundial_ef1_404.htm 21678 6745 /welcome/mundial_if_404.htm 4236 6746 /welcome/mundial_lf1_404.htm 21678 filename2: 6746 894694763 1... (2 Replies)
Discussion started by: jontjioe
2 Replies

6. Shell Programming and Scripting

Substituting variable value in AWK /start/,/stop/

Hi all u brilient people on the forum... I am trying to call the variable value in awk command for search pattern /start/,/stop/ but i am nt able to do this .... wat i did is ..i have created two variable YESTERDAY and TODAY and passed the y'day n 2'days dates in it...like this ... (14 Replies)
Discussion started by: whomi
14 Replies

7. UNIX for Dummies Questions & Answers

putting grep -c results number in a variable

I want to display "no results found" if a grep search of a name that the user inputs is not found anywhere in a certain file, Right now I have this, but doesn't seem to work. Im not sure what to change. read name results=grep -c $name file if ; then echo "No results found." exit... (1 Reply)
Discussion started by: busdude
1 Replies

8. Shell Programming and Scripting

need a little help with results from awk

Hi there all, I am using a line to get some replys from my PS I do ps -ef |awk '{printf $9}' But my result is 1 big line. No spaces between the lines or someting for example:... (2 Replies)
Discussion started by: draco
2 Replies

9. Shell Programming and Scripting

Stop awk adding a new line

In a loop, I want to append some text to a file without generating a new line (and then force a new line before re-iterating the loop). In the code below the first 'echo' command is OK as it uses '--n' for no new line. For the 'awk' line I *thought* I could solve it by using printf rather than... (1 Reply)
Discussion started by: TobyR
1 Replies
Login or Register to Ask a Question