How to grep for first entry in a file?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to grep for first entry in a file?
# 1  
Old 10-02-2014
How to grep for first entry in a file?

Hello friends, I have a question.

Sometimes I have to search for an entry in a file that is repeated thousands of times. Can you tell me how to search so that i get limited results?

For example:

Code:
file: myfile.txt

grep "hello world" myfile.txt

this above grep will generate 5000 results, scrolling the screen.

How can I view just the first 10?

Thanks again
# 2  
Old 10-02-2014
If I want the first 10 lines
Code:
 ${command that generates more results than I want} | head -10

And if I want the last 10
Code:
 ${command that generates more results than I want} | tail -10

This User Gave Thanks to Skrynesaver For This Post:
# 3  
Old 10-02-2014
Thank you friend
# 4  
Old 10-02-2014
Quote:
Originally Posted by Skrynesaver
Code:
 ${command that generates more results than I want} | head -10

This is of course correct, but - depending on the length of the file/stream to read - might consume considerable more time than necessary.

sed might be a viable alternative because it can be told to stop (even reading) after the first match:

Code:
sed -n '/<regexp to search for>/ {p;q;}' /path/to/file

I hope this helps.

bakunin


/PS: to verify what i have said above i used two small scripts, where one contains the sed-line (mysed.sh) and the other contains the grep-piped-in-head-line, each against the same file. Every line was executed 100 times in a loop. Here are the results, each script has been run times and the time of the third run was measured to alleviate effects of file caching.

Code:
# ./mygrep.sh > /dev/null ; ./mygrep.sh > /dev/null ; time ./mygrep.sh > /dev/null
real    0m0.20s
user    0m0.08s
sys     0m0.08s

# ./mysed.sh > /dev/null ; ./mysed.sh > /dev/null ; time ./mysed.sh > /dev/null   
real    0m0.14s
user    0m0.04s
sys     0m0.04s


Last edited by bakunin; 10-02-2014 at 11:07 AM..
# 5  
Old 10-02-2014
The grep -m option may be of some interest:
Quote:
-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines. If the input is standard input from a regular file, and NUM matching lines are output, grep ensures that the standard input is positioned to just after the last matching line before exiting, regardless of the presence of trailing context lines. This enables a calling process to resume a search. When grep stops after NUM matching lines, it outputs any trailing context lines. When the -c or --count option is also used, grep does not output a count greater than NUM. When the -v or --invert-match option is also used, grep stops after outputting NUM non-matching lines.
May not be available on your system...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to replace multiple "&nbsp;" entry with in <td> tag into single entry using sed?

I have the input file like this. Input file: 12.txt 1) There are one or more than one <tr> tags in same line. 2) Some tr tags may have one <td> or more tna one <td> tags within it. 3) Few <td> tags having "<td> &nbsp; </td>". Few having more than one "&nbsp;" entry in it. <tr> some td... (4 Replies)
Discussion started by: thomasraj87
4 Replies

2. How to Post in the The UNIX and Linux Forums

Help me, write a bash script to delete parent entry with all their child entry in LDAP UNIX server

Hi All, Please help me and guide me to write a bash/shell script on Linux box to delete parent entry with all their child entries. example: Parent is : ---------- dn: email=yogesh.kumar@wipro.com, o=wipro, o=in child is: ---------- dn: cn: yogesh kumar, email=yogesh.kumar@wipro.com,... (1 Reply)
Discussion started by: Chand
1 Replies

3. Shell Programming and Scripting

Need to grep certain entry out of log file

This one is a bit too challenging for me... Hopefully you guys can help. Let's say I have a log file called: "$MW_HOME/user_projects/domains/IDMDomain/servers/wls_ods?/logs/wls_ods1-diagnostic.log" In this log file I want to search for "DIP-10219". When I execute this $ cat... (7 Replies)
Discussion started by: exm
7 Replies

4. Shell Programming and Scripting

[awk] grep a part of filename as entry file

hi all, i need to combine these files into one csv file. Bounce_Mail_Event_Daily_Report_01_Jul_2012.csv Bounce_Mail_Event_Daily_Report_02_Jul_2012.csv Bounce_Mail_Event_Daily_Report_03_Jul_2012.csv Bounce_Mail_Event_Daily_Report_04_Jul_2012.csv... (10 Replies)
Discussion started by: makan
10 Replies

5. 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

6. UNIX Desktop Questions & Answers

How to grep for password file entry

How would I grep for password file entry without using grep 'username' /etc/passwd? perhaps with who? I want to create alias that will find the password file entry regardless of the user who is using it. Thanks (4 Replies)
Discussion started by: alis
4 Replies

7. UNIX for Dummies Questions & Answers

grep for password file entry

How would I grep for password file entry without using grep 'username' /etc/passwd? perhaps with who? I want to create alias that will find the password file entry regardless of the user who is using it. I am trying to get the same exact line from the file entry like: Name : Password : UserID... (7 Replies)
Discussion started by: alis
7 Replies

8. Shell Programming and Scripting

Not able to check for entry within file

Hi I am basically checking if a required entry exists within a file. In the code below I am fetching all directories of the format rel1, rela, rel3..etc..The same entries also exist in the sample log file. I want to check if entry in file1 exists within file2 but it is failing. Can you please... (4 Replies)
Discussion started by: swasid
4 Replies

9. Shell Programming and Scripting

Deleting file entry

Hello everyone, I want to compare the first line of a file(ABC) with that of a folder,XYZ(folder contents) and want that line to be deleted from the file(ABC) if that entry doesn't exist in the folder(XYZ) I want to put this in a loop. please can anyone help thanks (6 Replies)
Discussion started by: swasid
6 Replies

10. Shell Programming and Scripting

Grep table entry problem

Hi, I have a table which looks like this Line a b c 0 10 0 0 1 0 0 2 0 ... (1 Reply)
Discussion started by: ahjiefreak
1 Replies
Login or Register to Ask a Question