0
Help us understand the problem. What are the problem?

posted at

tsv を 1行1件ずつの json にする

コード

tsv2json.jq
(env.HEADER|split("\t")) as $header
| [ $header, split("\t") ]
| transpose
| map({(.[0]): .[1]})
| add
tsv2json.sh
#!/bin/bash
set -ue

read HEADER
export HEADER
jq -c -R -f tsv2json.jq

適当なtsvを

bash
$ { printf "c1\nc2\nc3\n"; seq -f '%04g' 30; } | paste - - -
c1  c2  c3
0001    0002    0003
0004    0005    0006
0007    0008    0009
0010    0011    0012
0013    0014    0015
0016    0017    0018
0019    0020    0021
0022    0023    0024
0025    0026    0027
0028    0029    0030

変換

bash
$ { printf "c1\nc2\nc3\n"; seq -f '%04g' 30; } | paste - - - | bash tsv2json.sh
{"c1":"0001","c2":"0002","c3":"0003"}
{"c1":"0004","c2":"0005","c3":"0006"}
{"c1":"0007","c2":"0008","c3":"0009"}
{"c1":"0010","c2":"0011","c3":"0012"}
{"c1":"0013","c2":"0014","c3":"0015"}
{"c1":"0016","c2":"0017","c3":"0018"}
{"c1":"0019","c2":"0020","c3":"0021"}
{"c1":"0022","c2":"0023","c3":"0024"}
{"c1":"0025","c2":"0026","c3":"0027"}
{"c1":"0028","c2":"0029","c3":"0030"}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
0
Help us understand the problem. What are the problem?