forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_to_json.sh
More file actions
executable file
·54 lines (52 loc) · 1.26 KB
/
csv_to_json.sh
File metadata and controls
executable file
·54 lines (52 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env bash
[[ -z $1 ]] && { echo "Usage: csv_to_json.sh CSV_FILE"; exit 1; }
export LANG=C
export LC_ALL=C
DELIM=$'\x1F'
set -o pipefail
sed -E \
':loop
s/^(([^"]*"[^"]*")*[^"]*),/\1'$DELIM'/;
t loop' \
$1 | \
awk -F$DELIM \
'BEGIN {
print "{"
} {
if (count == 0) {
for (i = 1; i <= NF; i++) {
names[i] = $i
}
} else if ($1 == "CORE:" || $1 == "LB:" || $1 == "PAR:") {
if (paramprinted) print "\n }"
else if (lineprinted) print ""
if (catprinted) print " },"
lineprinted = 0
paramprinted = 0
catprinted = 1
gsub(/:$/, "", $1)
print " \""$1"\": {";
} else if ($1 != "") {
if (lineprinted) print ""
if (paramprinted) print " },"
lineprinted = 0
paramprinted = 1
print " \""$1"\": {";
lineprinted = 0
for (i=2; i<=NF; i++) {
if ($i != "") {
gsub(/^"/, "", $i)
gsub(/"$/, "", $i)
gsub(/""/, "\"", $i)
if (lineprinted) print ","
lineprinted = 1
printf(" \"%s\": %s", names[i], $i)
}
}
}
count++;
} END {
if (paramprinted) print "\n }"
if (catprinted) print " }"
print "}"
}'