1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

Posted at

コード

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"}
1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?