I have a file called file.txt
. How can I print the first line only using the grep
command?

- 544,893

- 599
5 Answers
Although it's an unconventional application of grep, you can do it in GNU grep using
grep -m1 "" file.txt
It works because the empty expression matches anything, while -m1
causes grep to exit after the first match
-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines.

- 81,074
-
+1 for showing that chocolate-covered banana is not as useless here as I thought. – Kamil Maciorowski Jul 07 '16 at 19:20
-
3
This is not something grep
does. The name "grep" itself is an acronym for "globally search a regular expression and print", which is what the ed
command g/re/p
does (for a given regular expression re
).
ed
is an interactive line editor from 1969, but it's most likely installed on your system today nonetheless (it's a standard POSIX tool). We got grep
from ed
, and it can be seen as a shortcut or alias for a specific functionality of ed
, and sed
, which is "stream-ed
", i.e. a (non-interactive) stream editor.
Let's use sed
instead:
sed -n '1p' file.txt
The 1p
string is a tiny sed
"script" that prints (p
) the line corresponding to the given address (1
, the first line).
The editing command 1p
would (no surprise) do the same thing in the ed
editor by the way.
The -n
suppresses the output of anything not explicitly printed by the script, so all we get is the first line of the file file.txt
.
Alternatively:
sed '1q' file.txt
This prints all lines of the file, but quits (q
) at line 1 (after printing it). This is exactly equivalent to head -n 1 file.txt
.
In the POSIX standard (in the rationale section for the head
command) it says (paraphrasing) that head -n N
is much the same as sed 'Nq'
, i.e. "print every line, but quit at line N
". The reason head
was included in the standard at all was due to symmetry with tail
and backwards compatibility with existing Unix implementations.

- 333,661
-
1This is the most illuminating answer, the first answer is a weird hack. I knew there was something weird, thank you for showing me the history of grep and ed. It all makes sense now. – TZubiri Oct 02 '19 at 19:02
Unless the first line has a unique string you cannot do this using only grep. head -n 1 file.txt
would work in its place.
If you want to only print out the first line if it matches a pattern then pipe head into grep
head -n 1 * | grep [pattern]

- 106
Yet Another Unconventional Use of Grep -- a Schwartzian Transform that goes through several gyrations to number the lines, then uses grep to look for the line number, then strip the line number back off:
function grep1() (
nlines=$(wc -l < "$1")
nlw=$(printf "%d" "$nlines" | wc -c)
nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep '^1 ' | sed 's/^1 *//'
)
function greplast() (
nlines=$(wc -l < "$1")
nlines=$((nlines + 0))
nlw=$(printf "%d" "$nlines" | wc -c)
nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep "^$nlines " | sed "s/^$nlines *//"
)
I'm putting this Answer here as an example of the idea that just because you can do something in (grep or bash or ... etc), doesn't mean that you should -- there's probably a better tool for the job. sed (sed 1q
or sed -n 1p
) and head (head -n 1
) are the more appropriate tools for printing the first line from a file. For printing the last line of a file, you have tail -n 1
or sed -n '$p'
. Not only are these tools a single command (instead of 3+ in the above functions), they are also much clearer for future readers -- perhaps yourself! -- of the scripts they're in. While I am not one of the (currently 3) down-voters of your question, it's likely that your insistence on an arbitrary tool for the job (without any supporting reasons) is the reason for the downvotes. It's extremely unlikely that a system that has grep
does not also have head
, tail
, and sed
.

- 67,283
- 35
- 116
- 255
Here is what I used to get a header on my df output. Basically two commands run in sequence with the ";". df -h | head -n1; df -h | grep sda1

- 1
-
1Your answer would be much more readable with the code formatted in a code block with ``` surrounding it and possibly example output. – James Risner Oct 31 '22 at 15:18
-
Best to try and avoid running commands twice. In this case you could use awk to print the header and matching lines, e.g.
df -h | awk 'NR==1||/sda1/'
– mwfearnley Nov 28 '22 at 17:23
head -1 file.txt
not work? – clk Jul 07 '16 at 18:40grep
is not the best tool for printing the first line of a file. If you simply meant that you wanted to print the first line matched withgrep
, or if you have some specific use forgrep
, please let us know what that is. If we had more context, perhaps we could give an answer that would better help you and the community. – DKing Jul 07 '16 at 19:11