Tom Xue Tom Xue - 6 months ago 45
Linux Question

How to select the word from two associated lines?

I want to select the word form associated two lines. For example, below two lines will always appear together line by line. And the number 87 may change. I want to select the word/number 87, how to use sed or awk to do so?

string "abc"
var int 87


awk '/^string \"/ { getline ; print $3; }'

This says to match string " at the beginning of the line. Then get the next line and print the third word.

For example:

$ cat junk.txt
string "abc"
var int  87
some other thing
string "qwerty"
var int 99
$ cat junk.txt | awk '/^string \"/ { getline ; print $3; }'