Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

logging - Get line after matching pattern > 0 in linux

I have this log, and I want to print the lines with the "finished" status for the Jobs where "QTY REGS > 0" without using a script.

INFO  2021-01-19 02:00:00,001 JOB: 0970b711] starting
INFO  2021-01-19 02:00:01,001 JOB: 0970b711] adding records
INFO  2021-01-19 02:00:02,001 JOB: 0970b711] registered
INFO  2021-01-19 02:00:03,001 JOB: 0970b711] QTY REGS: 3 processing
INFO  2021-01-19 02:00:03,001 JOB: 32970g233] debug started
INFO  2021-01-19 02:00:04,001 JOB: 0970b711] finished
INFO  2021-01-19 02:00:05,001 JOB: 10d0g655] starting
INFO  2021-01-19 02:00:06,001 JOB: 10d0g655] QTY REGS: 0 processing
INFO  2021-01-19 02:00:07,001 JOB: 10d0g655] finished
INFO  2021-01-19 02:00:08,001 JOB: 0870a711] starting
INFO  2021-01-19 02:00:09,001 JOB: 0870a711] QTY REGS: 1 processing
INFO  2021-01-19 02:00:10,001 JOB: 0870a711] retry
INFO  2021-01-19 02:00:11,001 JOB: 0870a711] connect error
INFO  2021-01-19 02:00:12,001 JOB: 0870a711] finished

I would like to return for

INFO 2021-01-19 02:00:03,001 JOB: 0970b711] QTY REGS: 3 processing
INFO 2021-01-19 02:00:09,001 JOB: 0870a711] QTY REGS: 1 processing

That are the records where QTY REGS > 0 the lines with the same job number but that have "finished" as description

INFO 2021-01-19 02:00:04,001 JOB: 0970b711] finished
INFO 2021-01-19 02:00:12,001 JOB: 0870a711] finished

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suggest to use an array:

awk '$6=="QTY" && $8>0{a[$5]=1}; $6=="finished" && a[$5]==1{print}' file

Output:

INFO 2021-01-19 02:00:04,001 JOB: 0970b711] finished
INFO 2021-01-19 02:00:12,001 JOB: 0870a711] finished

{print} can also be omitted.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...