将一行中找到的值添加到与第一行的特定字段匹配的另一行

将一行中找到的值添加到与第一行的特定字段匹配的另一行

希望你们一切都好!

这是我想要实现的目标:

我有以下几行:

2022-10-19 09:12:39.174 WRITING bytes(2566), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:39.334 WRITING bytes(4301), indexname(20220601_cfgsys_017507001635)
2022-10-19 09:12:39.334 Sent bytes (2566) successfully
2022-10-19 09:12:39.334 WRITING bytes(1293), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:39.398 Sent bytes (4301) successfully
2022-10-19 09:12:39.398 WRITING bytes(7285), indexname(20220601_cfgsys_017507001635)
2022-10-19 09:12:39.890 Sent bytes (1293) successfully
2022-10-19 09:12:39.890 WRITING bytes(4289), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:40.123 Sent bytes (7285) successfully

我想取里面的值索引名匹配“WRITING”的行中的括号,并将其添加到行末尾匹配“Sent bytes”的行,仅当里面的数字字节括号匹配。

例如,所需的输出:

2022-10-19 09:12:39.174 WRITING bytes(2566), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:39.334 WRITING bytes(4301), indexname(20220601_cfgsys_017507001635)
2022-10-19 09:12:39.334 Sent bytes (2566) successfully 20220701_cfgsys_017507001635
2022-10-19 09:12:39.334 WRITING bytes(1293), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:39.398 Sent bytes (4301) successfully 20220601_cfgsys_017507001635
2022-10-19 09:12:39.398 WRITING bytes(7285), indexname(20220601_cfgsys_017507001635)
2022-10-19 09:12:39.890 Sent bytes (1293) successfully 20220701_cfgsys_017507001635
2022-10-19 09:12:39.890 WRITING bytes(4289), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:40.123 Sent bytes (7285) successfully 20220601_cfgsys_017507001635

我知道如何获取括号内的 2 个值(字节和索引名称):

cat test.txt | grep "WRITING" | awk -F"[()]" '{print $2" "$4}'

$2 = bytes 括号内的值 $4 = indexname 括号内的值

但是我无法将其添加到所需的行中,我需要在以下行(“WRITING”之后)中搜索字符串“Sent bytes”,并且 $2 等于“Sent bytes”行的 $2,然后添加$4 到“已发送字节”行末尾

任何帮助将不胜感激!

预先非常感谢您!

答案1

使用任何 awk:

$ cat tst.awk
BEGIN { FS = "[() ]+" }
($3 == "WRITING") && ($7 == "indexname") { idx[$5] = $8 }
(($3" "$4) == "Sent bytes") && ($5 in idx) { $0 = $0 OFS idx[$5] }
{ print }

$ awk -f tst.awk file
2022-10-19 09:12:39.174 WRITING bytes(2566), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:39.334 WRITING bytes(4301), indexname(20220601_cfgsys_017507001635)
2022-10-19 09:12:39.334 Sent bytes (2566) successfully 20220701_cfgsys_017507001635
2022-10-19 09:12:39.334 WRITING bytes(1293), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:39.398 Sent bytes (4301) successfully 20220601_cfgsys_017507001635
2022-10-19 09:12:39.398 WRITING bytes(7285), indexname(20220601_cfgsys_017507001635)
2022-10-19 09:12:39.890 Sent bytes (1293) successfully 20220701_cfgsys_017507001635
2022-10-19 09:12:39.890 WRITING bytes(4289), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:40.123 Sent bytes (7285) successfully 20220601_cfgsys_017507001635

答案2

使用awk

$ awk -F"[()]" '/indexname/{var=$4}/Sent bytes/{$(NF+1)=var}1' input_file
2022-10-19 09:12:39.174 WRITING bytes(2566), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:39.334 WRITING bytes(4301), indexname(20220601_cfgsys_017507001635)
2022-10-19 09:12:39.334 Sent bytes  2566  successfully 20220601_cfgsys_017507001635
2022-10-19 09:12:39.334 WRITING bytes(1293), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:39.398 Sent bytes  4301  successfully 20220701_cfgsys_017507001635
2022-10-19 09:12:39.398 WRITING bytes(7285), indexname(20220601_cfgsys_017507001635)
2022-10-19 09:12:39.890 Sent bytes  1293  successfully 20220601_cfgsys_017507001635
2022-10-19 09:12:39.890 WRITING bytes(4289), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:40.123 Sent bytes  7285  successfully 20220701_cfgsys_017507001635

相关内容