Using Grep to Get the Next Word After a Match in Each Line

Using grep to get the next WORD after a match in each line

Assuming you have gnu grep, you can use perl-style regex to do a positive lookbehind:

grep -oP '(?<=GET\s/)\w+' file

If you don't have gnu grep, then I'd advise just using sed:

sed -n '/^.*GET[[:space:]]\{1,\}\/\([-_[:alnum:]]\{1,\}\).*$/s//\1/p' file

If you happen to have gnu sed, that can be greatly simplified:

sed -n '/^.*GET\s\+\/\(\w\+\).*$/s//\1/p' file

The bottom line here is, you certainly don't need pipes to accomplish this. grep or sed alone will suffice.

Use grep to get next word after match

For "normal" integers and float values, you may use

grep -oP '(?<="USD":)\d+(?:\.\d+)?' file

If your numbers can have no integer part and can start with a ., use

grep -oP '(?<="USD":)\d*\.?\d+' file

An optional -:

grep -oP '(?<="USD":)-?\d*\.?\d+' file

See IDEONE demo

How to grep for contents after pattern?

grep 'potato:' file.txt | sed 's/^.*: //'

grep looks for any line that contains the string potato:, then, for each of these lines, sed replaces (s/// - substitute) any character (.*) from the beginning of the line (^) until the last occurrence of the sequence : (colon followed by space) with the empty string (s/...// - substitute the first part with the second part, which is empty).


grep 'potato:' file.txt | cut -d\   -f2

For each line that contains potato:, cut will split the line into multiple fields delimited by space (-d\ - d = delimiter, \ = escaped space character, something like -d" " would have also worked) and print the second field of each such line (-f2).


grep 'potato:' file.txt | awk '{print $2}'

For each line that contains potato:, awk will print the second field (print $2) which is delimited by default by spaces.


grep 'potato:' file.txt | perl -e 'for(<>){s/^.*: //;print}'

All lines that contain potato: are sent to an inline (-e) Perl script that takes all lines from stdin, then, for each of these lines, does the same substitution as in the first example above, then prints it.


awk '{if(/potato:/) print $2}' < file.txt

The file is sent via stdin (< file.txt sends the contents of the file via stdin to the command on the left) to an awk script that, for each line that contains potato: (if(/potato:/) returns true if the regular expression /potato:/ matches the current line), prints the second field, as described above.


perl -e 'for(<>){/potato:/ && s/^.*: // && print}' < file.txt

The file is sent via stdin (< file.txt, see above) to a Perl script that works similarly to the one above, but this time it also makes sure each line contains the string potato: (/potato:/ is a regular expression that matches if the current line contains potato:, and, if it does (&&), then proceeds to apply the regular expression described above and prints the result).

get the next word after grep matching

iw dev wlan0 station dump | grep -Po '(?<=Station\s|signal avg:\s)[^\s]*'

This regexp uses a so-called lookbehind syntax. You can read about it here

Example output:



Thanks for voting this answer up. Now I know another solution:

iw dev wlan0 station dump | grep -Po '(Station\s|signal avg:\s)\K[^\s]*'

Which is actually a shorthand for the solution above. \K basically means "forget everything before its occurance".

Grep next word after pattern match

Referencing @sundeep answer

grep -Eo '"name":"[^"]+"'

resulted in the expected output

Grep and print only matching word and the next words

This is arguably, easier to do with sed:

sed -n 's/.*EndPatch, //p' install.history

to get the word after EndPatch:

sed -n 's/.*EndPatch, \([^,]*\).*/\1/p' install.history


sed -n 's/.*EndPatch, //p' install.history | cut -d, -f

grep after match

$ grep -Po '(?<=myname\s)\w+' inputFile

Use grep to extract first word after match

Don't use grep. This is tailor-made job for Awk:

awk '$1 == "FROM" { print $2 }'

EDIT Thanks to @rojo for this suggestion

awk 'BEGIN{FS="from|FROM|where|WHERE"} /from|FROM/ {print $2}'

EDIT 2: WIth filename and line #

awk 'BEGIN{FS="from|FROM|where|WHERE"}
/from|FROM/ {printf ("%s:%d:%s\n", FILENAME, NR, $2)}'

grep match next word after pattern until first space

You could use awk if you are pretty sure if the words are de-limited by space, since by default awk splits up fields in input line by a white-space characters. For your given input all you need is

awk '{ for( i=1; i<=NF ;i++ ) if ( $i == "MODULE" ) { print $(i+1); break } }' 

The for loop just runs up to NF which basically means run till the last row entry in the current line split by the white-space character.

If you are still persistent on using grep, you could improve the regex by doing below. In PCRE you can use ? to match the variable number of white-space characters by doing (\s+)? and get only the part without the white-space.

grep -oP '(?<=MODULE)(\s+)?\K([^ ]*)'

See the Regular Expression from regex101 working for your given input.

Related Topics

Leave a reply