158

I have very long log files, is it possible to ask grep to only search the first 10 lines?

0

11 Answers 11

219

The magic of pipes;

head -10 log.txt | grep <whatever>
3
  • 18
    you can also pipe an arbitrary stream to head: someCmd | head -10 May 30, 2015 at 8:19
  • 1
    Head defaults to printing the first 10 lines to standard output, so this is valid for 10 lines head log.txt | grep <whatever>
    – Zlemini
    Sep 30, 2016 at 12:38
  • 6
    Is there a way to do this when using grep's -l option? I'd like to list all the files who's first 5 characters are RIFFD. May 23, 2017 at 19:20
63

For folks who find this on Google, I needed to search the first n lines of multiple files, but to only print the matching filenames. I used

gawk 'FNR>10 {nextfile} /pattern/ { print FILENAME ; nextfile }' filenames

The FNR..nextfile stops processing a file once 10 lines have been seen. The //..{} prints the filename and moves on whenever the first match in a given file shows up. To quote the filenames for the benefit of other programs, use

gawk 'FNR>10 {nextfile} /pattern/ { print "\"" FILENAME "\"" ; nextfile }' filenames
5
  • 13
    I was one of those folks who found this on Google. Thanks!
    – Floris
    Dec 17, 2013 at 15:06
  • 1
    for me, this code printed out the full path of the file. Which is exactly what I needed. Also FNR=1 will just search 1st line. Thanks!
    – Brian W
    Oct 12, 2017 at 5:25
  • 7
    To do this recursively over a directory: find ./path -type -f -exec awk 'FNR>10 {nextfile} /pattern/ { print FILENAME ; nextfile }' '{}' +
    – OrangeDog
    May 7, 2019 at 17:17
  • 2
    Thanks @OrangeDog. One slight correction: should be -type f Sep 18, 2019 at 19:51
  • Just what I was searching for!
    – anthony
    Aug 11, 2021 at 7:00
28

Or use awk for a single process without |:

awk '/your_regexp/ && NR < 11' INPUTFILE

On each line, if your_regexp matches, and the number of records (lines) is less than 11, it executes the default action (which is printing the input line).

Or use sed:

sed -n '/your_regexp/p;10q' INPUTFILE 

Checks your regexp and prints the line (-n means don't print the input, which is otherwise the default), and quits right after the 10th line.

5
  • 1
    Why not quit on the 10th? (see sed solution)
    – potong
    Jan 6, 2012 at 23:56
  • awk '{ if ( NR <= 10 ) { if(index($0,"ab") > 0) { print $0; } } else { exit; } }' textfile -- faster.
    – user982733
    Jan 7, 2012 at 3:07
  • 1
    @potong you are right, corrected. @srikanthradix while it can be faster you're solution is not searching for regexps but only for fixed strings. awk '{ if ( NR <= 10 ) { if( $0 ~ "YOUR_REGEXP") { print } } else { exit; } }' textfile does. Jan 7, 2012 at 10:06
  • 4
    Plus the style isn't awkish. 2xifs and 1xelse in a command that needs no action statement would make aho. weinberger and kernighan cry ... Jan 7, 2012 at 12:46
  • 1
    I think, instead of NR it would be better to use FNR, because if you use awk with multiple files FNR starts from 0 for each file. Oct 4, 2016 at 9:09
10

You have a few options using programs along with grep. The simplest in my opinion is to use head:

head -n10 filename | grep ...

head will output the first 10 lines (using the -n option), and then you can pipe that output to grep.

1
  • 7
    I didn't even realize, all the solutions here using head have used -n 10 (including me) not realizing that head by default displays only 10 lines. :) Jan 7, 2012 at 1:05
4
grep "pattern" <(head -n 10 filename)
4
head -10 log.txt | grep -A 2 -B 2 pattern_to_search

-A 2: print two lines before the pattern.

-B 2: print two lines after the pattern.

head -10 log.txt # read the first 10 lines of the file.
1
  • 1
    If I recall, -C 2 will do the same as -A 2 -B 2 Jan 24, 2020 at 16:59
3

You can use the following line:

head -n 10 /path/to/file | grep [...]
3

The output of head -10 file can be piped to grep in order to accomplish this:

head -10 file | grep …

Using Perl:

perl -ne 'last if $. > 10; print if /pattern/' file
3

An extension to Joachim Isaksson's answer: Quite often I need something from the middle of a long file, e.g. lines 5001 to 5020, in which case you can combine head with tail:

head -5020 file.txt | tail -20 | grep x

This gets the first 5020 lines, then shows only the last 20 of those, then pipes everything to grep.

(Edited: fencepost error in my example numbers, added pipe to grep)

0

grep -A 10 <Pattern>

This is to grab the pattern and the next 10 lines after the pattern. This would work well only for a known pattern, if you don't have a known pattern use the "head" suggestions.

2
  • 1
    Although it might right. add more description of question to make answer more comprehensive. Sep 18, 2014 at 8:37
  • 5
    This answers a completely different question and is not useful in this context.
    – Pre101
    Oct 18, 2018 at 3:43
-3
grep -m6 "string" cov.txt

This searches only the first 6 lines for string

1
  • 12
    No, this will give you the first 6 occurrences of "string" in the whole cov.txt file Oct 31, 2019 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.