Dilip
Dilip

Reputation: 11

Find the word after the pattern using Awk

I have executed the below command and i have stored the data in sample.txt file.

chkconfig --list | grep postfix > sample.txt

Now this file contains the below output :

postfix         0:off   1:off   2:on    3:on    4:on    5:on    6:off

Now I need to extract the string after 2:. The output should be on

Is the anyway we can find it using awk or grep?

Upvotes: 1

Views: 4701

Answers (2)

anubhava
anubhava

Reputation: 784928

awk can do that:

 awk -F '[: ]+' '{print $7}'

Also you don't need a grep. Following awk will do the job:

chkconfig --list | awk -F '[: ]+' '$1 == "postfix" {print $7}'

Upvotes: 1

jaypal singh
jaypal singh

Reputation: 77075

One way with awk bypassing the temp file creation if extraction is the only purpose of the temp file:

chkconfig --list | awk '/postfix/{$0=substr($4,3)}1'

Upvotes: 2

Related Questions