Can I grep only the first n lines of a file?
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)
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, 2020Comments
-
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 over 12 yearsWhy not quit on the 10th? (see sed solution)
-
jaypal singh over 12 yearsI didn't even realize, all the solutions here using
head
have used-n 10
(including me) not realizing thathead
by default displays only 10 lines. :) -
Admin over 12 years
awk '{ if ( NR <= 10 ) { if(index($0,"ab") > 0) { print $0; } } else { exit; } }' textfile
-- faster. -
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 over 12 yearsPlus the style isn't
awkish
.2xifs
and1xelse
in a command that needs no action statement would make aho. weinberger and kernighan cry ... -
Floris over 10 yearsI was one of those folks who found this on Google. Thanks!
-
Pramod S. Nikam almost 10 yearsAlthough it might right. add more description of question to make answer more comprehensive.
-
Stuart Nelson about 9 yearsyou can also pipe an arbitrary stream to
head
:someCmd | head -10
-
Zlemini over 7 yearsHead defaults to printing the first 10 lines to standard output, so this is valid for 10 lines
head log.txt | grep <whatever>
-
Vladyslav Savchenko over 7 yearsI 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 about 7 yearsIs 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 areRIFFD
. -
Brian W over 6 yearsfor 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 over 5 yearsThis answers a completely different question and is not useful in this context.
-
OrangeDog about 5 yearsTo do this recursively over a directory:
find ./path -type -f -exec awk 'FNR>10 {nextfile} /pattern/ { print FILENAME ; nextfile }' '{}' +
-
David Siegal almost 5 yearsThanks @OrangeDog. One slight correction: should be
-type f
-
franzisk over 4 yearsNo, this will give you the first 6 occurrences of "string" in the whole cov.txt file
-
David LeBauer over 4 yearsIf I recall,
-C 2
will do the same as-A 2 -B 2
-
anthony almost 3 yearsJust what I was searching for!