简体   繁体   English

awk根据条件打印特定列

[英]awk to print specific columns based on conditions

So I have this text from a grep: 所以我从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. 我想在一行中唤醒它,如果可能的话,获取第二列CLASS行和第12列INFO行。

I am in a SunOS 5.10 sun4v sparc SUNW . 我在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

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. 请注意,此条件下您之前的egrep "INFO|CLASS "可能也是不必要的。


To produce a compact output, just catch the value when finding "CLASS" and print it when in "INFO": 要生成一个紧凑的输出,只需在找到“CLASS”时捕获该值,并在“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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM