I have a file (file.txt) like:
field1|field2|field3
field1|field2|field3
field1|field2|field3
(actually something like 83 fields, 200k rows) I need to convert it into something like:
field3.field2|{"myname1":"field1","myname2":"field2","myname3":"field3"}
field3.field2|{"myname1":"field1","myname2":"field2","myname3":"field3"}
field3.field2|{"myname1":"field1","myname2":"field2","myname3":"field3"}
I'm currently doing it in several steps, but it takes ages:
cat file.txt |jq -nR '[inputs | split("|") | {
"myname1": .[0],
"myname2": .[1],
"myname3": .[2]}]' > /var/tmp/tempfile.txt
cat /var/tmp/tempfile.txt | jq -c '.[]' |while read row;do
key=$(echo "$row"|jq -r '(.myname3) "." (.myname2)')
echo "$key|$row"
done
is there a better way with jq or something else (under Linux) to achieve the same goal ?
thanks
CodePudding user response:
Here's a solution using jq
with string interpolation and conversion to JSON using tojson
:
jq -Rr './"|" | "\(.[2]).\(.[1])|\({
"myname1": .[0],
"myname2": .[1],
"myname3": .[2]
} | tojson)"' file.txt
field3.field2|{"myname1":"field1","myname2":"field2","myname3":"field3"}
field3.field2|{"myname1":"field1","myname2":"field2","myname3":"field3"}
field3.field2|{"myname1":"field1","myname2":"field2","myname3":"field3"}