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

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

我需要对文件进行一些格式化,以将 then 行的列附加到具有相同特定列的另一个行。需要匹配的列是第一列(数字)和类型(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

答案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

相关内容