Tom Xue Tom Xue - 1 year ago 75
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


Answer Source
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; }'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download