1

I have the following content and a file called file.txt

TIMESTAMP   200 J8899
INSTANCES   19  x86_64  False   i-xxx1a1a   t2.medium   True    subnet-93b224fb hvm vpc-7b801
STATE   16  running
TIMESTAMP   300 J2099
INSTANCES   18  x86_64  False   i-xxx1a1b   t2.medium   True    subnet-93b224fb hvm vpc-7b801
STATE   16  running

Now I want to print the following output

i-xxx1a1a : running
i-xxx1a1b : running

Please note line starting with TIMESTAMP, INSTANCES, STATE are new lines

I tried doing this but it didn't work

grep -Ew "INSTANCES|STATE" file.txt | awk '{print $0}'

1 Answer 1

3

You can just use awk:

awk '/INSTANCES/{ thing = $5; } /STATE/ { print thing,":",$NF; }' file.txt

This assumes that relevant STATE always appears after INSTANCE. Also if there is a missing STATE for an INSTANCES it'll skip the whole instance.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .