Yoni Levy Yoni Levy - 4 months ago 7
Bash Question

Getting text after a match from stdout

I want to extract the next word/number after specific words I find using grep or whatnot.

As an example lets say this is what I have in stdout

string-a0 match-a1 string-a2 string-a3
string-b0 string-b1 match-b2 string-b3
match-c0 string-c1 string-c2 string-c3


I want to be left with just this

string-a2
string-b3
string-c1


mind that
match-a1 != match-b2 != match-c0


EDIT

A concrete example...

stdout is this

open 0.23 date ...
close 1.52 date ...
open 2.34 date ...
close 5.92 date ...
open 10.78 date ...
close 24.21 date ...
total 45.3


I'm searching for the words
open
,
close
and
total
so the output should be

0.23
1.52
2.34
5.92
10.78
24.21
45.3

Answer

This doesn't match the general case, but works for your example:

awk '/^open|^close|^total/{print $2}' input

For the general case, if your definition of "string" is based on whitespace, perhaps you want:

tr -s ' \n' \\n < input | awk 'p{print; p=0} {p=/^open|^close|^total/}'