When you only want the slice of a line that comes after some anchor text, you usually pair a regex anchor with either GNU grep -oP or awk. This page collects the common shapes: the next word, the rest of the line, a fixed number of characters, and the next line after a match. For wider regex practice on the command line, see grep pattern scenarios.
Tested the commands in this article on Ubuntu 25.04 with GNU grep 3.11 and mawk 1.3.4 (2026-06-13). Examples that use
-Pneed GNU grep; BSD/macOS grep does not implement-P.
Grep word after match and grep after match (\K)
\K “drops” everything matched so far, so -o prints only the part after the anchor. That is the usual answer for “grep after match” when the anchor is literal text.
Print the first word after the word love (space required so you do not match lovely):
echo "I love apples." | grep -oP 'love\s+\K\S+'apples.Print the rest of the line after love (same as “grep everything after match” on one line):
echo "I love apples." | grep -oP 'love\K.*'apples.Note the leading space: the pattern love\K.* keeps whatever follows love literally. To skip one space after love, use love\s+\K.*.
Grep everything after match
“Everything after” is either “rest of this line” or “rest of string after a delimiter”. For a delimiter such as =:
echo "key=value rest" | grep -oP 'key=\K.*'value restIf the anchor can appear more than once, decide whether you want the first or last occurrence; grep -oP works line by line, and greedy .* runs to the end of the line.
Grep characters after match
To grab exactly N characters after the anchor (“grep characters after match”), use {N} after \K:
echo "Color: Blue" | grep -oP 'Color:\K.{3}'BlHere {3} counts the space after the colon and the first two letters of Blue. Tighten the pattern if you want to skip whitespace: for example Color:\s*\K.{3} → Blu.
Grep next line after match
-A (“after context”) prints the matching line plus N following lines. That is the straightforward way to implement “grep next line after match”.
printf '%s\n' 'before' 'MATCH this' 'next line only' | grep -A1 'MATCH'MATCH this
next line onlyTo keep only the line after the match:
printf '%s\n' 'before' 'MATCH this' 'next line only' | grep -A1 'MATCH' | tail -n1next line onlyawk can do the same without tail:
printf '%s\n' 'before' 'MATCH this' 'next line only' | awk '/MATCH/{getline; print; exit}'next line onlyAwk print after match
When the line is field-oriented, awk often beats grep: match a pattern, then print a later field.
echo "I love apples" | awk '/love/{print $3}'applesWhen the anchor is not a field separator, use match and substr so you print from the character after the match (portable POSIX awk):
echo "I love apples" | awk 'match($0,/love[[:space:]]+/) {print substr($0, RSTART+RLENGTH)}'applesSplitting on a delimiter is another common “awk print after match” pattern:
echo "Fruit: Apple" | awk -F': ' '/Fruit/{print $2}'AppleMore awk building blocks: awk examples.
Word before a match (short)
The same grep -oP style works with a lookahead so you print text before a fixed word:
echo "Apples are sweet." | grep -oP '\S+(?= are)'Applesecho "I love apples." | grep -oP '.*(?= apples)'I loveWith awk, fields before a keyword are often easiest by splitting on : or another delimiter, or by match on the keyword and substr on 1, RSTART-1. Pick grep for regex-heavy “slice this string”, and awk when you already think in columns.
Summary
“Grep word after match” and related grep-after-match jobs on Linux are usually grep -oP 'Anchor\s*\K…': -o prints only the match, -P enables \K and lookarounds, and \S+, .*, or .{N} choose the next word, the rest of the line, or a fixed number of characters. For “grep everything after match” on a single line, Anchor\K.* (optionally with \s+) is enough. For “grep next line after match”, use grep -A1 and then drop the first line (tail -n1) or use awk with getline. When you need field math or match/substr, awk is often the maintainable tool. Remember -P is GNU grep–specific; elsewhere use sed or awk only, or install ggrep.
Further reading: grep: next word after match, next word after grep matching, grep next words, grep next word after match.

