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?


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
  • 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
  • 10
    What should I do, if I don't want to print the line with the pattern match? – tommy.carstensen Jul 6 '17 at 12:29
  • 7
    @tommy.carstensen: sed '/PATTERN/Q' FILE will skip the matched line. Q is a GNU extension, so it won't work with any sed. – Alex O Sep 8 '18 at 13:37

print up to and including the match:

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

print up to BUT NOT including the match:

awk '/pattern/ {exit} {print}' filename
sed '/pattern/Q' filename
  • 14
    Q's cool but gnu specific afaik, sed -n '/pattern/!p;//q' would be more portable. – don_crissti Apr 6 '15 at 13:36
  • @don_crissti: you should make it an answer, I think it's a good one (: I am a little curious how it works, though. I believe ! makes p apply to lines not matching pattern, but then the //q confuses me... – jwd Aug 22 '19 at 1:25
  • 2
    @don_crissti: ah, I got it — // means "previous regular expression" (I thought it meant "match the empty string"). I think a shorter version of the same solution is: sed -n '/pattern/q;p ? – jwd Aug 22 '19 at 1:39
  • @jwd - indeed, that's shorter. 👍 – don_crissti Aug 22 '19 at 19:30

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
  • 3
    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
  • 5
    What should I do, if I don't want to print the line with the pattern match? – tommy.carstensen Jul 6 '17 at 12:29

For people who choose to remember only the basic tools in day to day work, and willing to accept less elegant and less efficient solutions:

head -n $(grep -n pattern filename | cut -d: -f1) filename

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.

  • 1
    Nice idea, but three commands when one will do. – Mikel Apr 14 '11 at 9:47
  • 1
    Knowing the basics is very good indeed. Knowing the right tool for the job is better, though. – soulmerge Apr 14 '11 at 15:00

Adding onto Mikel's answer above...

To print all lines up to, but not including, the first line in FILE containing PATTERN, try:

  • sed '/.*PATTERN.*/{s///;q;}' FILE

This matches the entire line containing the pattern, replaces it with a blank line, then quits without processing the rest of the file.


The easiest/clearest way I could think of to prevent printing an extra newline at the end (without involving another tool), was to run sed again and delete the new final line:

sed '/.*PATTERN.*/{s///;q;}' FILE | sed '$d'

... and since we're now deleting that line anyway, our previous work is redundant and we can simplify to:

sed '/PATTERN/q' FILE | sed '$d'
  • Glenn's answer - and my comment there - show how to do it with a single sed invocation. – don_crissti Dec 12 '17 at 20:27
  • (Thanks for that - I did see your comment on agc's answer but either missed the other one or just skimmed it because my brain dislikes double-negatives.) Since I was using this in both a tcsh and a bash alias, I needed to make sure I had a relatively concise one-line solution that worked in both standard and GNU sed (for portability); all requirements that your contribution may very well have met. As someone who uses sed very rarely, my most important requirement was for something that I could quickly understand when I want to easily edit or re-purpose it years from now. – Jim Grisham Dec 14 '17 at 19:02

The following pure GNU grep methods are not efficient.

Search for everything up to the first instance of string "foo" in file bar, using three greps:

grep -m 1 -B $(grep -n -m 1 foo bar | grep -o '^[0-9]*') foo bar

Matching up to the last instance of "foo":

grep -oPz "(?s)[^\n]*${s}.*?\n.*?foo.*?\n" bar

Note: details on the last grep can be found in: Regex (grep) for multi-line search needed.

  • Why would one ever want to use 7 greps (+ pcre) when it's a matter of simply running a single sed invocation: sed 'x;/./G;//!x;/foo/p;//s/.*//;x;d' ?? – don_crissti Oct 14 '16 at 13:02
  • @don_crissti, your sed code seems worth its own answer, or could be added to one of the others. Re 7 greps: Because there was no grep answer... (plus, the answer helps show why not.) – agc Oct 14 '16 at 18:21
  • It's not my code, just click on it... Even if that was my code it does not answer the Q here so I wouldn't post it as an answer. – don_crissti Oct 14 '16 at 18:25

You could also use one of the following

tac ./test | grep -B $(cat ./test | wc -l) -m 1 'pattern'|tac 


tac ./test |head -n $(tac ./test | grep -n 'pattern' | cut -d: -f1 | head -n 1)|tac


tac ./test |sed ':a;N;$!ba;s/\n/'"pattern"'/g' | sed 's/'"patternpattern"'/\n/g'|head -n 1|sed 's/'"pattern"'/\n/g'|tac

The first option is very similar to what the OP suggested only it makes sure ti show enough lines before context by counting the lines in the file

The second option searches the line number of the first match (you could change that as well by changing the inner 'head') and than uses head on that number

The last option replaces all new lines with the match and than replaces two adjacent matches with a new line. The output of this is a line for every block of text between two matches. After that it uses 'head' to choose the first line (mwaning the block of text until the first match) match and than retranslates each match to a new line. this option works only if the file is in the following format

texttexttext texttexttext

and so forth

  • 2
    consider explaining how these work, especially because that sed command at the bottom is kind of gnarly. – strugee Jul 9 '15 at 19:45
  • the first option is very similar to what the OP suggested only it makes sure ti show enough kines before context by counting the lines in the filr, – user122778 Jul 10 '15 at 5:49

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.