I need to remove all lines after the first line that contains the word "fox"
Say 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.
For searching purposes I will add this line:
Remove lines after occurrence
Later edit:
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:
How to delete all the lines after the last occurence of pattern?
How to delete lines before a match perserving it?
Remove all lines before a match with sed
How to delete the lines starting from the 1st line till line before encountering the pattern '[ERROR] -17-12-2015' using sed?
How to delete all lines before the first and after the last occurrence of a string?
awk '{print} /fox/{exit}' file
With GNU sed:
sed '0,/fox/!d' file
or
sed -n '0,/fox/p' file
2.1m questions
2.1m answers
60 comments
56.8k users