如何解析和替换awk中的GPU日志?

时间:2019-11-08 20:22:51

标签: regex awk

我有grep输出:

Nov 07 16:03:23 SA1789 miner[1397]: GPU4 not responding                   
Nov 07 16:03:23 SA1789 miner[1397]: Thread(s) not responding. Restarting.
Nov 07 16:02:25 SA0888 miner[1505]: GPU12 not responding                            
Nov 07 16:02:25 SA0888 miner[1505]: Thread(s) not responding. Restarting.           
Nov 07 16:04:17 SA0888 miner[1505]: GPU12 not responding                       
Nov 07 16:04:17 SA0888 miner[1505]: Thread(s) not responding. Restarting.

想要得到这个:

Nov 07 16:03:23 ==> GPU4
Nov 07 16:02:25 ==> GPU12
Nov 07 16:04:17 ==> GPU12
awk '/GPU/{s=index($7,":"); $7=substr($7,s+1,length($7)-s-1); print $1" "$2" "$3" => "$7}'

很抱歉,我只是菜鸟,只需要专家的快速解决方案即可。

1 个答案:

答案 0 :(得分:4)

简单的awk表达式:

$ awk '/GPU/{ print $1, $2, $3, "==>", $6 }' file
Nov 07 16:03:23 ==> GPU4
Nov 07 16:02:25 ==> GPU12
Nov 07 16:04:17 ==> GPU12