如何通过 Linux 命令将修复消息解析为 JSON 格式
How to parse fix message to JSON format by Linux command
我已经修复了需要转换为 JSON 格式的消息文件,如下所示。我如何使用 shell 脚本来转换这些?
发件人:
05/03 11:23:19.123456 << 8=FIX.4.2^9=451^35=D^49=abc^56=bcd
05/03 11:23:19.123457 << 8=FIX.4.2^9=451^35=D^49=abc1^56=bcd1
05/03 11:23:19.123458 << 8=FIX.4.2^9=451^35=D^49=abc2^56=bcd2
收件人:
{"time":"05/03 11:23:19.123456", "8":"FIX.4.2", "35":"D", "49":"abc", "56":"bcd"}
{"time":"05/03 11:23:19.123457", "8":"FIX.4.2", "35":"D", "49":"abc1", "56":"bcd1"}
{"time":"05/03 11:23:19.123458", "8":"FIX.4.2", "35":"D", "49":"abc2", "56":"bcd2"}
我认为在所需输出中遗漏 9=451
是个意外。
$ awk 'BEGIN{FS="\^|[ <]+"}{printf "{\"time\":\"%s %s\"",, ; for(i=3;i<=NF;i++){split($i,a,/=/);printf ", \"%s\":\"%s\"",a[1],a[2]}; printf "\n"}' fix
{"time":"05/03 11:23:19.123456", "8":"FIX.4.2", "9":"451", "35":"D", "49":"abc", "56":"bcd"
{"time":"05/03 11:23:19.123457", "8":"FIX.4.2", "9":"451", "35":"D", "49":"abc1", "56":"bcd1"
{"time":"05/03 11:23:19.123458", "8":"FIX.4.2", "9":"451", "35":"D", "49":"abc2", "56":"bcd2"
这里有注释来解释它是如何工作的:
# BEGIN rule(s)
BEGIN {
FS = "\^|[ <]+" # define two field separators, ^ and any number or consecutive spaces or left angle brackets
}
# Rule(s)
{
printf "{\"time\":\"%s %s\"", , # printing the time (which we treat separately as it's internally "split" by a space.
for (i = 3; i <= NF; i++) { # the other fields we can iterate over, NF in awk is the 'number of fields'
split($i, a, /=/) # use the loop variable i to reference the fields, split them into an array on the '='
printf ", \"%s\":\"%s\"", a[1], a[2] # print the array elements formatted, all on one line
}
printf "\n" # add a new line when we're done
}
我已经修复了需要转换为 JSON 格式的消息文件,如下所示。我如何使用 shell 脚本来转换这些?
发件人:
05/03 11:23:19.123456 << 8=FIX.4.2^9=451^35=D^49=abc^56=bcd
05/03 11:23:19.123457 << 8=FIX.4.2^9=451^35=D^49=abc1^56=bcd1
05/03 11:23:19.123458 << 8=FIX.4.2^9=451^35=D^49=abc2^56=bcd2
收件人:
{"time":"05/03 11:23:19.123456", "8":"FIX.4.2", "35":"D", "49":"abc", "56":"bcd"}
{"time":"05/03 11:23:19.123457", "8":"FIX.4.2", "35":"D", "49":"abc1", "56":"bcd1"}
{"time":"05/03 11:23:19.123458", "8":"FIX.4.2", "35":"D", "49":"abc2", "56":"bcd2"}
我认为在所需输出中遗漏 9=451
是个意外。
$ awk 'BEGIN{FS="\^|[ <]+"}{printf "{\"time\":\"%s %s\"",, ; for(i=3;i<=NF;i++){split($i,a,/=/);printf ", \"%s\":\"%s\"",a[1],a[2]}; printf "\n"}' fix
{"time":"05/03 11:23:19.123456", "8":"FIX.4.2", "9":"451", "35":"D", "49":"abc", "56":"bcd"
{"time":"05/03 11:23:19.123457", "8":"FIX.4.2", "9":"451", "35":"D", "49":"abc1", "56":"bcd1"
{"time":"05/03 11:23:19.123458", "8":"FIX.4.2", "9":"451", "35":"D", "49":"abc2", "56":"bcd2"
这里有注释来解释它是如何工作的:
# BEGIN rule(s)
BEGIN {
FS = "\^|[ <]+" # define two field separators, ^ and any number or consecutive spaces or left angle brackets
}
# Rule(s)
{
printf "{\"time\":\"%s %s\"", , # printing the time (which we treat separately as it's internally "split" by a space.
for (i = 3; i <= NF; i++) { # the other fields we can iterate over, NF in awk is the 'number of fields'
split($i, a, /=/) # use the loop variable i to reference the fields, split them into an array on the '='
printf ", \"%s\":\"%s\"", a[1], a[2] # print the array elements formatted, all on one line
}
printf "\n" # add a new line when we're done
}