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

  • 4
    Would head -1 file.txt not work? – clk Jul 7 '16 at 18:40
  • @steeldriver you need to make it an answer as it is the way to do it with grep, strangely selected tool for the job. – MelBurslan Jul 7 '16 at 19:00
  • 1
    grep 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 with grep, or if you have some specific use for grep, 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 7 '16 at 19:11
  • @MelBurslan In file.txt I have three lines for example : This is a new file. Second line: The name of the file is newFile Third line : I have not created the new line. So by using grep command how can I print the first line only? Also How which command will help me to print both first and last line? Can you guys please tell me the commands for both the questions? – Nithin Santhosh Jul 8 '16 at 10:59
  • If any of the existing answers solves your problem, please consider accepting it via the checkmark. Thank you! – Jeff Schaller Apr 25 '17 at 10:40

Although it's an unconventional application of grep, you can do it 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.
| improve this answer | |
  • +1 for showing that chocolate-covered banana is not as useless here as I thought. – Kamil Maciorowski Jul 7 '16 at 19:20
  • 4
    That might work on some systems, but it's not portable: grep: unknown option -- m – Kusalananda Jul 7 '16 at 19:21
  • 1
    Note the difference between 'first line' and 'first line matched' – Charles Apr 7 '18 at 22:10

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.


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.

| improve this answer | |
  • 1
    This 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. – Tomas Zubiri Oct 2 '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]

| improve this answer | |

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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