I have fix message file that would needed to be converted to JSON format, like below. How can I use a shell script to convert these?
From:
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
To:
{"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"}
Advertisement
Answer
I assume that the omission of 9=451
in the desired output was an accident.
$ awk 'BEGIN{FS="\^|[ <]+"}{printf "{"time":"%s %s"",$1, $2; 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"
And here annotations to explain how it works:
# 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"", $1, $2 # 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 }