Table of Contents
There are various ways to print next word after pattern match or previous word before pattern match in Linux but in this article we will focus on grep and awk command along with other regex.
We will cover below topics in this article
- Print next word after pattern match
- Print last word before pattern match
- Print everything in line after pattern match
- Print content between two matched pattern
With grep we can use lookahead to lookbehind.
With positive lookahead q(?=u)
matches q that is followed by a u, without making the u part of the match.
The construct for positive lookbehind is (?<=text)
a pair of parentheses, with the opening parenthesis followed by a question mark, “less than” symbol, and an equals sign.
1. Print next word after pattern match using grep
1.1 Using lookbehind
Let's look at some examples to print next word after pattern match. In the below example I print next word after pattern match i.e. field3
# echo "field1 field2 field3 field4" | grep -oP '(?<=field3 )[^ ]*'
field4
Similarly to print next word after pattern match of field2
# echo "field1 field2 field3 field4" | grep -oP '(?<=field2 )\w+'
field3
1.2 Using perl extended pattern
Now we will use perl extended pattern to print next word after pattern match.
# echo "field1 field2 field3 field4" | grep -oP 'field3 \K\w+'
field4
Here \K
means keep the stuff left of the \K
, don't include it in $&
and \w
means match a "word
" character
Some more examples:
# echo "field1 field2 field3 field4" | grep -oP 'field1 \K\w+' field2 # echo "field1 field2 field3 field4" | grep -oP 'field2 \K\w+' field3
2. Print next word after pattern match
2.1 Using awk
We showed various examples to print next word after pattern match using grep, now we will do the same with awk
# echo "field1 field2 field3 field4" | awk '{if(/field2/) print $4 " " $2}' field4 field2 # echo "field1 field2 field3 field4" | awk '{if(/field2/) print $4}' field4
Some more examples:
If first field is field1
then print $2
# echo "field1 field2 field3 field4" | awk '$1 == "field1" {print $2}' field2 # echo "field1 field2 field3 field4" | awk '$2 == "field2" {print $3}' field3
3. Print everything in line after pattern match
Now with lookbehind
I showed examples to print next word after pattern match but to print everything in line after pattern match
# echo "field1 field2 field3 field4" | grep -oP '(?<=field2 ).*'
field3 field4
Some more example with little tweak
# echo "field1 field2 : field3 field4" | grep -oP '(?<=field2\s: )[^ ]*'
field3
Here we use \s to match whitespace character
4. Print content between two matched pattern
Now we can use both lookahead
and lookbehind
to print content between two matched pattern. Here I wish to print text between field1
and field3
# echo "field1 field2 field3 field4" | grep -oP '(?<=field1 )\w+(?= field3)'
field2
We can do the same using perl extended patterns
# echo "field1 field2 field3 field4" | grep -oP 'field1 \K\w+(?= field3)'
field2
5. Print last word before pattern match using grep with lookahead
Similar to lookbehind
we can use lookahead
to print last word before pattern match using grep
# echo "field1 field2 field3 field4" | grep -oP '\w+(?= field3)'
field2
6. Print everything in line before pattern match
To print everything in line before pattern match in Linux we can use lookahead
with below regex
# echo "field1 field2 field3 field4" | grep -oP '.*(?= field3)'
field1 field2
7. Print everything in line after pattern match
Now you can also use lookbehind
or other regex to print everything in line after pattern match
# echo "field1 field2 field3 field4" | grep -o "field2.*"
field2 field3 field4
To print everything on line before match
# echo "field1 field2 field3 field4" | grep -o ".*field2"
field1 field2
Lastly I hope the examples from this article to grep and print next word after pattern match on Linux was helpful. So, let me know your suggestions and feedback using the comment section.
Fantastic… Countless hours saved… This post should be top on Google.. Thank you
Priceless! Thank you!