Xao
Xao

Reputation: 591

awk to print specific columns based on conditions

So I have this text from a grep:

$ bppllist -allpolicies -l | egrep  'INFO|CLASS '
CLASS DEV_DC1_MSSQL_Daily *NULL* 0 760000 0 *NULL*
INFO 15 0 0 3590 *NULL* 0 0 2147483647 0 0 0 0 0 0 0 0 0 0 1359375508 D2135AE2694411E293B100C0DD0FD650 1 0 0 0 0 0 0 0 2 0 0 0 0 0 0 3 0 93 0 0 0 0 0 0 1
CLASS DEV_DC1_MSSQL_Daily_TL *NULL* 0 760000 0 *NULL*
INFO 15 0 0 3590 *NULL* 0 0 2147483647 0 0 0 0 0 0 0 0 0 0 1359375508 538E4964610F11E5B3CB00C0DD0FD650 1 0 0 0 0 0 0 0 2 0 0 0 0 0 0 3 0 50 0 0 0 0 0 0 1
CLASS DEV_DC1_MSSQL_Monthly *NULL* 0 760000 0 *NULL*
INFO 15 0 0 3590 *NULL* 0 0 2147483647 0 0 0 0 0 0 0 0 0 0 1359375508 53B1F616610F11E5A02E00C0DD0FD650 1 0 0 0 0 0 0 0 2 0 0 0 0 0 0 3 0 58 0 0 0 0 0 0 1

And I want to awk this in one line, if possible to get the 2nd column of CLASS rows and the 12th column of INFO rows.

I am in a SunOS 5.10 sun4v sparc SUNW.

I tried to do it like this:

bppllist -allpolicies -l | egrep  'INFO|CLASS ' | awk '($1 == "CLASS ") && ($2 == "INFO") { print $2 $12}'

But I think it's just a conditioning not double filtering.

Also, is there any way to get them in this format?

DEV_DC1_MSSQL_Daily 0
DEV_DC1_MSSQL_Daily_TL 0
DEV_DC1_MSSQL_Monthly 0

Upvotes: 3

Views: 3943

Answers (1)

fedorqui
fedorqui

Reputation: 289505

Just say so:

$ awk '$1=="CLASS" {print $2} $1=="INFO" {print $12}' file
DEV_DC1_MSSQL_Daily
0
DEV_DC1_MSSQL_Daily_TL
0
DEV_DC1_MSSQL_Monthly
0

Note by the way that your previous egrep "INFO|CLASS " may be also unnecessary with this condition.


To produce a compact output, just catch the value when finding "CLASS" and print it when in "INFO":

$ awk '$1=="CLASS" {c=$2} $1=="INFO" {print c, $12}' file
DEV_DC1_MSSQL_Daily 0
DEV_DC1_MSSQL_Daily_TL 0
DEV_DC1_MSSQL_Monthly 0

Upvotes: 4

Related Questions