Cruhrup96
Cruhrup96

Reputation: 3

Awk Match String and Print ID String

I am in the process of deleting old KMS keys on our system and learning bash. This forum has been super helpful with suggestions!

I am wanting to print key ID's of keys created in the year 2019. Here is my filter so far..

Input:

./ksctl keys list -l 100 | egrep -w 'id|name|createdAt' | awk '$2 ~ /"2019/ { print }' > keylist.txt

Output:

"createdAt": "2019-06-20T20:15:20Z",
"createdAt": "2019-06-20T20:15:20Z",
"createdAt": "2019-06-20T20:15:20Z",
"createdAt": "2019-06-20T20:15:20Z",
"createdAt": "2019-06-20T20:15:20Z",
etc..

This is wonderful, however I also need the KeyID printed with this match.

Desired Output:

"createdAt": "2019-06-20T20:15:20Z",
"id": "asdf1234",

Upvotes: 0

Views: 98

Answers (1)

Barmar
Barmar

Reputation: 781848

if the id line is always after the createdAt line, use getline; to read it.

./ksctl keys list -l 100 | 
    egrep -w 'id|name|createdAt' | 
    awk '$2 ~ /"2019/ { print; getline; print }' > keylist.txt

Upvotes: 1

Related Questions