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

BashSearchGrep

Bash Problem Overview


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

Bash Solutions


Solution 1 - Bash

The magic of pipes;

head -10 log.txt | grep <whatever>

Solution 2 - Bash

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 - Bash

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 - Bash

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 - Bash

grep "pattern" <(head -n 10 filename)

Solution 6 - Bash

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.

Solution 7 - Bash

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

Solution 8 - Bash

You can use the following line:

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

Solution 9 - Bash

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)

Solution 10 - Bash

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.

Solution 11 - Bash

grep -m6 "string" cov.txt

This searches only the first 6 lines for string

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid LeBauerView Question on Stackoverflow
Solution 1 - BashJoachim IsakssonView Answer on Stackoverflow
Solution 2 - BashcxwView Answer on Stackoverflow
Solution 3 - BashZsolt BotykaiView Answer on Stackoverflow
Solution 4 - BashDan FegoView Answer on Stackoverflow
Solution 5 - Bashjaypal singhView Answer on Stackoverflow
Solution 6 - BashvinsView Answer on Stackoverflow
Solution 7 - BashAlan Haggai AlaviView Answer on Stackoverflow
Solution 8 - BashGustavo StraubeView Answer on Stackoverflow
Solution 9 - BashRoGView Answer on Stackoverflow
Solution 10 - BashsnowtopView Answer on Stackoverflow
Solution 11 - BashDileepa ChandimaView Answer on Stackoverflow