Awk

在文件中附加具有相同鍵的列

  • April 15, 2020

我需要為我的文件設置一些格式,以便將然後行的列附加到具有相同特定列的另一行。需要匹配的列是第一列(數字)和類型(Sword_SC、Fire_ball_sc、Fire_sc)。請注意,如果第一列和時間戳相同,但類型如“FIRE%”,則它們仍需要組合為 1 個襯裡。

非常感謝您的幫助。

樣本文件:

09123456789|Alarm|Alarm 2020-04-15 00:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1
09123456789|Alarm|Alarm 2020-04-15 04:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1
09123456789|Alarm|Alarm 2020-04-15 15:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1
09123456789|Alarm|Alarm 2020-04-15 16:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1
09123456789|Alarm|Alarm 2020-04-15 20:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1
09222222222|Alarm|Alarm 2020-04-15 00:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1
09222222222|Alarm|Alarm 2020-04-15 04:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1
09222222222|Alarm|Alarm 2020-04-15 15:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1
09222222222|Alarm|Alarm 2020-04-15 16:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1
09222222222|Alarm|Alarm 2020-04-15 20:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1
09555555555|Alarm|Alarm 2020-04-15 20:59:59^Breached Threshold^FIRE_BALL_SC @ 0.0 ^Sample Size: 1
09555555555|Alarm|Alarm 2020-04-15 20:59:59^Breached Threshold^FIRE_SC @ 0.0 ^Sample Size: 1

渴望輸出

09123456789|Alarm|Alarm 2020-04-15 00:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1^^Alarm 2020-04-15 04:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1^^Alarm 2020-04-15 15:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1^^Alarm 2020-04-15 16:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1^^Alarm 2020-04-15 20:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1
09222222222|Alarm|Alarm 2020-04-15 00:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1^^Alarm 2020-04-15 04:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1^^Alarm 2020-04-15 15:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1^^Alarm 2020-04-15 16:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1^^Alarm 2020-04-15 20:59:59^Breached Threshold^Sword_SC @ 0.0 ^Sample Size: 1
09555555555|Alarm|Alarm 2020-04-15 20:59:59^Breached Threshold^FIRE_BALL_SC @ 0.0 ^Sample Size: 1^^Alarm 2020-04-15 20:59:59^Breached Threshold^FIRE_SC @ 0.0 ^Sample Size: 1

GNU awk

gawk -F '|' -v OFS='|' '
 {
   n = split($3, fields, /\^/)
   key = $1 substr(fields[3], 1, 4)
   alarms[key] = alarms[key] sep[key] $3
   sep[key] = "^^"
 }
 END {
   PROCINFO["sorted_in"] = "@ind_num_asc"
   for (key in alarms) print gensub(/....$/, "", 1, key), "Alarm", alarms[key]
 }
' file

引用自:https://unix.stackexchange.com/questions/580253