How to remove all lines after a line containing some string?

Viewed 409

I need to remove all lines after the first line that contains the word "fox".

For example, for the following input file "in.txt":

The quick
brown fox jumps
over
the
lazy dog
the second fox
is hunting

The result will be:

The quick
brown fox jumps

I prefer a script made in awk or sed but any other command line tools are good, like perl or php or python etc.

I am using gnuwin32 tools in Windows, and the solution I could find was this one:

grep -m1 -n fox in.txt | cut -f1 -d":" > var.txt
set /p MyNumber=<var.txt
head -%MyNumber% in.txt > out.txt

However, I am looking for a solution that is shorter and that is portable (this one contains Windows specific command set /p).

Similar questions:

1 Answers
awk '{print} /fox/{exit}' file

With GNU sed:

sed '0,/fox/!d' file

or

sed -n '0,/fox/p' file
Related