How can I print the contents of a file minus everything including and below the line above the first occurrence of a pattern?
Say the pattern is "^Previous" on line 15; then I would like to print lines 1--13.
How can I print the contents of a file minus everything including and below the line above the first occurrence of a pattern?
Say the pattern is "^Previous" on line 15; then I would like to print lines 1--13.
sed
can do this all by itself:
sed -n '$!N;/.*PATTERN.*/q;P;D' infile
It's very simple: turn on silent mode, use a sliding window (via N
and D
, so that there are always two lines in the pattern space), q
uit if encountering PATTERN
otherwise P
rint the first line in the pattern space and restart the cycle.
With gnu sed
it's even shorter as it can Q
uit without auto-printing so you can skip silent mode:
sed '$!N;/.*PATTERN.*/Q;P;D' infile