파일에 동일한 키를 가진 열 추가

파일에 동일한 키를 가진 열 추가

동일한 특정 열이 있는 다른 행에 다음 행의 열을 추가하려면 파일에서 일부 형식을 지정해야 합니다. 일치해야 하는 열은 첫 번째 열(번호)과 유형(Sword_SC, Fire_ball_sc, Fire_sc)입니다. 첫 번째 열과 타임스탬프가 동일하지만 "FIRE%"와 같은 유형인 경우에도 여전히 하나의 패드로 결합해야 합니다.

도움을 주셔서 감사합니다.

샘플 파일:

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

관련 정보