符合条件的awk打印行-大于

符合条件的awk打印行-大于,awk,Awk,上面的命令生成以下结果: cat file.txt | awk ' /queryId/; /stats_missing/; /memory/' 我想要的是只生成内存>特定数字(ex 300)的结果 所以我的输出只会产生内存:(大于300的数字) file.txt中的示例源数据 queryId : d94bf4e4756e6342 stats_missing : false memory : 500 根据输入数据的结构,有许多方法可以做到这一点 假设stats\u missing和

上面的命令生成以下结果:

cat file.txt |  awk  ' /queryId/; /stats_missing/; /memory/'
我想要的是只生成内存>特定数字(ex 300)的结果 所以我的输出只会产生内存:(大于300的数字)

file.txt中的示例源数据

queryId : d94bf4e4756e6342
  stats_missing : false
  memory : 500

根据输入数据的结构,有许多方法可以做到这一点

假设
stats\u missing
memory
行总是出现在
queryId
之后,您可以执行以下操作:

queryId : 794d12600c5b11ad:c79abeee00000000
statement : SELECT `e88172_FIRSTFORBS_103118`.`filedt` `e88172_FIRSTFORBS_103118`.`ssn` `e88172_FIRSTFORBS_103118`.`suff` `e88172_FIRSTFORBS_103118`.`ln_num` `e88172_FIRSTFORBS_103118`.`prog` `e88172_FIRSTFORBS_103118`.`dlqdys` `e88172_FIRSTFORBS_103118`.`status` `e88172_FIRSTFORBS_103118`.`currprin` `e88172_FIRSTFORBS_103118`.`origprin` `e88172_FIRSTFORBS_103118`.`rpmtbegdt` `e88172_FIRSTFORBS_103118`.`forbmos` `e88172_FIRSTFORBS_103118`.`owner` `e88172_FIRSTFORBS_103118`.`frstdsbdt` `e88172_FIRSTFORBS_103118`.`uniq_loan_id` `e88172_FIRSTFORBS_103118`.`fdr_acct_nbr` FROM `edw_sandbox`.`e88172_FIRSTFORBS_103118` `e88172_FIRSTFORBS_103118`  LIMIT 10000
queryType : QUERY
queryState : FINISHED
startTime : 2018-12-19T22:29:02.235Z
rowsProduced : null
attributes : {
  thread_cpu_time_percentage : 51
  thread_network_receive_wait_time : 34
  thread_cpu_time : 166
  hdfs_average_scan_range : 3.3535640666666668E7
  bytes_streamed : 3572722
  hdfs_bytes_read : 100606922
  query_status : OK
  hdfs_scanner_average_bytes_read_per_second : 1.358740106189443E9
  thread_network_receive_wait_time_percentage : 11
  oom : false
  planning_wait_time_percentage : 0
  admission_wait : 0
  connected_user : e88172
  stats_missing : false
  planning_wait_time : 9
  client_fetch_wait_time_percentage : 0
  session_type : HIVESERVER2
  memory : 629145600
  client_fetch_wait_time : 6
  file_formats :
  admission_result : Admitted immediately
  pool : root.default
  session_id : e3463e2b18af960e:56a
  stats_corrupt : false
  thread_total_time : 329
  thread_network_send_wait_time_percentage : 5
  impala_version : impalad version 2.10.0-cdh5.13.3 RELEASE (build 15a453e15865344e75ce0fc6c4c760696d50f626)
  network_address : 172.19.79.81:44886
  thread_storage_wait_time_percentage : 34
  thread_network_send_wait_time : 15
  thread_storage_wait_time : 112
这将存储与
查询
统计信息缺失
匹配的行以及
内存
的值,然后在填充所有3行且
内存
值超过5000(在本例中)时,将其打印出来

例如

如前所述,如果其他两行在下一个查询ID之前不总是存在,那么这可能会导致所有问题


如果不是这样的话,你必须做一些事情,比如每次遇到一个新的queryId时都要跟踪,然后重置变量。或者根据上次遇到的queryId,将所有queryId、stats_missing和内存数据存储在一个数组中,然后在最后打印所有内容。有很多方法可以剥下这只猫的皮,这取决于。

使用多行的
Perl肯定很容易:

$perl-0777-lnE'while(/^(queryId[\s\s]+?^\h*内存\h*:\h*)(\d+)/mg){如果$2300}文件为“$1$2”
查询ID:3440b6c90a6ccec1
统计数据缺失:正确
内存:500

@oguzismail提供了示例。如果您满意,请向上投票如果您发布示例源数据,也许会得到更好的答案。很可能源代码中已有记录结构。@karakfa谢谢,我发布了示例源数据
queryId : 794d12600c5b11ad:c79abeee00000000
statement : SELECT `e88172_FIRSTFORBS_103118`.`filedt` `e88172_FIRSTFORBS_103118`.`ssn` `e88172_FIRSTFORBS_103118`.`suff` `e88172_FIRSTFORBS_103118`.`ln_num` `e88172_FIRSTFORBS_103118`.`prog` `e88172_FIRSTFORBS_103118`.`dlqdys` `e88172_FIRSTFORBS_103118`.`status` `e88172_FIRSTFORBS_103118`.`currprin` `e88172_FIRSTFORBS_103118`.`origprin` `e88172_FIRSTFORBS_103118`.`rpmtbegdt` `e88172_FIRSTFORBS_103118`.`forbmos` `e88172_FIRSTFORBS_103118`.`owner` `e88172_FIRSTFORBS_103118`.`frstdsbdt` `e88172_FIRSTFORBS_103118`.`uniq_loan_id` `e88172_FIRSTFORBS_103118`.`fdr_acct_nbr` FROM `edw_sandbox`.`e88172_FIRSTFORBS_103118` `e88172_FIRSTFORBS_103118`  LIMIT 10000
queryType : QUERY
queryState : FINISHED
startTime : 2018-12-19T22:29:02.235Z
rowsProduced : null
attributes : {
  thread_cpu_time_percentage : 51
  thread_network_receive_wait_time : 34
  thread_cpu_time : 166
  hdfs_average_scan_range : 3.3535640666666668E7
  bytes_streamed : 3572722
  hdfs_bytes_read : 100606922
  query_status : OK
  hdfs_scanner_average_bytes_read_per_second : 1.358740106189443E9
  thread_network_receive_wait_time_percentage : 11
  oom : false
  planning_wait_time_percentage : 0
  admission_wait : 0
  connected_user : e88172
  stats_missing : false
  planning_wait_time : 9
  client_fetch_wait_time_percentage : 0
  session_type : HIVESERVER2
  memory : 629145600
  client_fetch_wait_time : 6
  file_formats :
  admission_result : Admitted immediately
  pool : root.default
  session_id : e3463e2b18af960e:56a
  stats_corrupt : false
  thread_total_time : 329
  thread_network_send_wait_time_percentage : 5
  impala_version : impalad version 2.10.0-cdh5.13.3 RELEASE (build 15a453e15865344e75ce0fc6c4c760696d50f626)
  network_address : 172.19.79.81:44886
  thread_storage_wait_time_percentage : 34
  thread_network_send_wait_time : 15
  thread_storage_wait_time : 112
awk -F':' '/queryId/ {query=$0}
     /stats_missing/ {stats=$0}
     /memory/ {mem=$2}
     query && stats && mem>5000 {
        print query;print stats; print "memory: "mem; query=stats=mem="";}'
$ cat mem.txt
queryId : 3440b6c90a6ccec1
  stats_missing : true
  memory : 493719877394422178
queryId : d94bf4e4756e6342
  stats_missing : false
  memory : 2214592512
queryId : 3440b6c90a6ccec1
  stats_missing : true
  memory : 4978
queryId : d94bf4e4756e6342
  stats_missing : false
  memory : 200


$ awk -F':' '/queryId/ {query=$2}
     /stats_missing/ {stats=$2}
     /memory/ {mem=$2}
     query && stats && mem>5000 {
        print "queryId:"query;print "stats_missing:"stats; print "memory:"mem; query=stats=mem="";}' mem.txt
queryId: 3440b6c90a6ccec1
stats_missing: true
memory: 493719877394422178
queryId: d94bf4e4756e6342
stats_missing: false
memory: 2214592512
$ perl -0777 -lnE 'while (/^(queryId[\s\S]+?^\h*memory\h*:\h*)(\d+)/mg){ say "$1$2" if $2<300}' file
queryId : d94bf4e4756e6342
    stats_missing : false
    memory : 200

$ perl -0777 -lnE 'while (/^(queryId[\s\S]+?^\h*memory\h*:\h*)(\d+)/mg){ say "$1$2" if $2>300}' file
queryId : 3440b6c90a6ccec1
    stats_missing : true
    memory : 500