get value after specific word

Using grep, it can be done as follows:

grep -oP "from\s+\K\w+" input.txt

Here,

-o  ==>  option for printing only the matching part of the line
-P  ==>  use perl-regexp
\K  ==>  do not print that comes before \K (zero-width look-behind assertion)
\w  ==>  match word characters

Or:

awk '{for (I=1;I<NF;I++) if ($I == "from") print $(I+1)}' file

A readable solution would be:

awk -F '${fixed_string}' '{print $2}' file | awk '{print $1}'

What it does:

  • -F '${fixed_string}' separates the input into before and after the given string. So with your file, when we set fixed_string='from', print $2 would give:

    California 12 58 Nevada 12 5 8 95 2 48 5 NY 5 6 845 156 585 Miami

  • Now, all you need is the 1st column of this input. So we pipe the output of the first awk to awk and print the 1st column.

Tags:

String

Shell