This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
va/Assignment2_part1/convert.sh

31 lines
826 B
Bash
Raw Permalink Normal View History

2023-04-24 07:49:04 +00:00
#!/bin/sh
set -e
SCRIPT_DIR=$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd)
input="$SCRIPT_DIR/data/restaurants.csv"
output="$SCRIPT_DIR/data/restaurants.jsonl"
# In order:
# - Convert CSV to JSON
# - Convert JSON array in JSON lines notation
# - Remove last line (which is all `null`)
cat "$input" | jq -s --raw-input --raw-output \
'split("\n") | .[1:-1] | map(split(",")) |
map({
"id": .[0],
"name": .[1],
"city": .[2],
"location": {
"lon": .[8] | sub("^\"\\["; "") | sub("\\s*"; "") | tonumber,
"lat": .[9] | sub("\\]\"$"; "") | sub("\\s*"; "") | tonumber,
},
"averageCostForTwo": .[3],
"aggregateRating": .[4],
"ratingText": .[5],
"votes": .[6],
"date": .[7]
})' "$input" | \
jq -c '.[]' > "$output"