Only print output after finding pattern

AWK can do this with pattern ranges, which allows the use of any regular expression:

echoer | awk '/pattern/,0'

will print echoer’s output starting with the first line matching pattern.

AWK is pattern-based, and is typically used with a “if this pattern matches, do this” type of approach. “This pattern” can be a range of patterns, defined as “when this pattern matches, start doing this, until this other pattern matches”; this is specified by writing two patterns separated by a comma, as above. Patterns can be text matches, as in /pattern/, where the current line is checked against the pattern, interpreted as a regular expression; they can also be general expressions, evaluated for every line, and considered to match if their result is non-zero or non-empty.

In AWK, the default action is to print the current line.

Putting all this together, awk '/pattern/,0' looks for lines matching pattern, and once it finds one, applies the default action to all lines until the 0 condition matches (is non-zero). awk '/pattern/,""' would work too.

The Gawk manual goes into much more detail.


The obligatory sed equivalent of @StephenKitt's awk one:

sed '/pattern/,$!d'

pattern there is interpreted as a Basic Regular Expression like in grep (as opposed to Extended Regular Expression in awk/egrep/grep -E). Some sed implementations have a -E (BSD, ast, recent GNU/busybox, soon POSIX) or -r (GNU, ssed, busybox, some recent BSD) option to make it Extended Regular Expressions instead and some have -P (ast) or -R (ssed) to make it a perl-like regular expression.

With perl:

perl -ne 'print if /pattern/ .. undef'

with GNU and *BSD grep:

grep -A1000000000 pattern file

Unless your file has more than 1M lines, that's it.

Tags:

Printf

Echo