grep lines after match until the end

With GNU grep (tested with version 2.6.3):

git status | grep -Pzo '.*Untracked files(.*\n)*'

Uses -P for perl regular expressions, -z to also match newline with \n and -o to only print what matches the pattern.

The regex explained:

First we match any character (.) zero or multiple times (*) until an occurence of the string Untracked files. Now, the part inside the brackets (.*\n) matches any character except a newline (.) zero or multiple times (*) followed by a newline (\n). And all that (that's inside the backets) can occure zero or multiple times; that's the meaning of the last *. It should now match all other lines, after the first occurence of Untracked files.


If you don't mind using sed, here is a possible solution

git status | sed -n -e '/Untracked files/,$p'

I would use awk for this:

git status | awk '/Untracked files/,0'

This /Untracked files/,0 is a range exression. It evaluates to True from the first time Untracked files and until 0 evaluates to True. Since this never happens, it prints the rest of the file.

Note that awk's behaviour to a True is to print the current record (line, normally), that's why we don't have to explicitly call print.


You may try:

git status | grep -A99 Untracked

where -A would print lines of trailing context after the match.

Alternatively use more or less, for example:

git status | less +/Untracked

For scripting purposes, I'd use ex/vi:

git status | ex -s +"/Untracked/norm d1G" +%p -cq! /dev/stdin

To include line with Untracked files, use kd1G instead of d1G.

Or without piping and /dev/stdin, the syntax would be: ex ... <(git status).