Can I grep only the first n lines of a file?

215,241

Solution 1

The magic of pipes;

head -10 log.txt | grep <whatever>

Solution 2

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

Solution 3

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.

Solution 4

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.

Solution 5

grep "pattern" <(head -n 10 filename)
Share:
215,241
David LeBauer
Author by

David LeBauer

I am a scientist interested in optimizing agricultural systems to maximize yield while reducing inputs and enhancing ecosystem services. Many of my questions on SO and related sites are related to the development of software, databases, and statistical analyses to support scientific research aimed at predicting ecosystem response to global change and engineering new systems for sustainable food and fuel production. Key projects include: TERRA REF An open access data and computing platform to support high throughput penotyping web github PEcAn (Predictive Ecosystem Analyzer), a model-data synthesis framework: code; project website; web interface BETYdb, A database of plant traits, crop yields, and computational provenance code; web interface BioCro: crop productivity and ecosystem services simulator: code Ecosystem Climate Regulation Services Calculator: code; demo

Updated on July 05, 2020

Comments

  • David LeBauer
    David LeBauer almost 4 years

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

  • potong
    potong over 12 years
    Why not quit on the 10th? (see sed solution)
  • jaypal singh
    jaypal singh over 12 years
    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. :)
  • Admin
    Admin over 12 years
    awk '{ if ( NR <= 10 ) { if(index($0,"ab") > 0) { print $0; } } else { exit; } }' textfile -- faster.
  • Zsolt Botykai
    Zsolt Botykai over 12 years
    @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.
  • jaypal singh
    jaypal singh over 12 years
    Plus the style isn't awkish. 2xifs and 1xelse in a command that needs no action statement would make aho. weinberger and kernighan cry ...
  • Floris
    Floris over 10 years
    I was one of those folks who found this on Google. Thanks!
  • Pramod S. Nikam
    Pramod S. Nikam almost 10 years
    Although it might right. add more description of question to make answer more comprehensive.
  • Stuart Nelson
    Stuart Nelson about 9 years
    you can also pipe an arbitrary stream to head: someCmd | head -10
  • Zlemini
    Zlemini over 7 years
    Head defaults to printing the first 10 lines to standard output, so this is valid for 10 lines head log.txt | grep <whatever>
  • Vladyslav Savchenko
    Vladyslav Savchenko over 7 years
    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.
  • James M. Lay
    James M. Lay about 7 years
    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.
  • Brian W
    Brian W over 6 years
    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!
  • Pre101
    Pre101 over 5 years
    This answers a completely different question and is not useful in this context.
  • OrangeDog
    OrangeDog about 5 years
    To do this recursively over a directory: find ./path -type -f -exec awk 'FNR>10 {nextfile} /pattern/ { print FILENAME ; nextfile }' '{}' +
  • David Siegal
    David Siegal almost 5 years
    Thanks @OrangeDog. One slight correction: should be -type f
  • franzisk
    franzisk over 4 years
    No, this will give you the first 6 occurrences of "string" in the whole cov.txt file
  • David LeBauer
    David LeBauer over 4 years
    If I recall, -C 2 will do the same as -A 2 -B 2
  • anthony
    anthony almost 3 years
    Just what I was searching for!