Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

grep --before-context 5 shows 5 lines before the match.

I want to show everything before the match.
Doing grep --before-context 99999999 would work but it is not very... professional.

How to show all the file up to the match?

share|improve this question

4 Answers 4

up vote 18 down vote accepted

Sed is better for that.

Just do:


It works like this:

For each line, we look if it matches /PATTERN:

  • if yes, we print it and quit
  • otherwise, we print it

This is the most efficient solution, because as soon as it sees PATTERN, it quits. Without q, sed would continue to read the rest of the file, and do nothing with it. For big files it can make a difference.

This trick can also be used to emulate head:

sed 10q FILE
share|improve this answer
Just tried, it just outputs the first line of the file... even though match is at line 38. –  Nicolas Raoul Apr 14 '11 at 9:44
Works fine for me. Can you give an example of actual input and output? And the command you are running as-is. –  Mikel Apr 14 '11 at 9:46
I had tried your command before you edited it, it was: sed '/PATTERN/p;q' FILE –  Nicolas Raoul Apr 14 '11 at 9:51

sed can replace most of grep's functionality.

sed -n '1,/<pattern>/ p' <file>

This means print from the first line until pattern is matched.

A couple of range examples

sed -n '/<pattern>/,$ p' <file> # from pattern to end of file
sed -n '/<pattern1>/,/<pattern2>/ p' <file> # from pattern1 to pattern2
share|improve this answer
Thanks it works! –  Nicolas Raoul Apr 14 '11 at 9:49
This command is good, but you can do better. This way it reads the whole file, but it is possible to quit as soon as it has found a match. –  Mikel Apr 14 '11 at 9:55

print up to and including the match:

awk '{print} /pattern/ {exit}' filename

print up to BUT NOT including the match:

awk '/pattern/ {exit} {print}' filename

Disclaimer: I know awk better than sed -- I bet there is a sed way to do both.

share|improve this answer

For people like me, who choose to remember only the basic usage of tools, and willing to accept less elegant and less efficient solutions:

head -n $(grep -n pattern filename | cut -d: -f1) filename
share|improve this answer
Nice idea, but three commands when one will do. –  Mikel Apr 14 '11 at 9:47
Knowing the basics is very good indeed. Knowing the right tool for the job is better, though. –  soulmerge Apr 14 '11 at 15:00
If this command is for a script then I will look for more elegant (and possibly efficient) solutions. If this is a one time command (or a throw away script), then I don't care. –  lesmana Apr 14 '11 at 17:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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