Grep word after match; awk print after match (Linux)

Grep word after match, grep everything after match, grep characters after match, grep next line after match, and awk print after match on Linux (GNU grep -P with \K, -A, match/substr). Brief patterns for the word before a match.

Published

Updated

Read time 4 min read

Reviewed byDeepak Prasad

Grep word after match; awk print after match (Linux)

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 -P need 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):

bash
echo "I love apples." | grep -oP 'love\s+\K\S+'
text
apples.

Print the rest of the line after love (same as “grep everything after match” on one line):

bash
echo "I love apples." | grep -oP 'love\K.*'
text
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 =:

bash
echo "key=value rest" | grep -oP 'key=\K.*'
text
value rest

If 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:

bash
echo "Color: Blue" | grep -oP 'Color:\K.{3}'
text
Bl

Here {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”.

bash
printf '%s\n' 'before' 'MATCH this' 'next line only' | grep -A1 'MATCH'
text
MATCH this
next line only

To keep only the line after the match:

bash
printf '%s\n' 'before' 'MATCH this' 'next line only' | grep -A1 'MATCH' | tail -n1
text
next line only

awk can do the same without tail:

bash
printf '%s\n' 'before' 'MATCH this' 'next line only' | awk '/MATCH/{getline; print; exit}'
text
next line only

Awk print after match

When the line is field-oriented, awk often beats grep: match a pattern, then print a later field.

bash
echo "I love apples" | awk '/love/{print $3}'
text
apples

When the anchor is not a field separator, use match and substr so you print from the character after the match (portable POSIX awk):

bash
echo "I love apples" | awk 'match($0,/love[[:space:]]+/) {print substr($0, RSTART+RLENGTH)}'
text
apples

Splitting on a delimiter is another common “awk print after match” pattern:

bash
echo "Fruit: Apple" | awk -F': ' '/Fruit/{print $2}'
text
Apple

More 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:

bash
echo "Apples are sweet." | grep -oP '\S+(?= are)'
text
Apples
bash
echo "I love apples." | grep -oP '.*(?= apples)'
text
I love

With 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.

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …