Skip to content

Instantly share code, notes, and snippets.

@ziggahunhow
Last active October 21, 2022 06:04
Show Gist options
  • Save ziggahunhow/91c88e162f00c608c6fb8fc8af1671cf to your computer and use it in GitHub Desktop.
Save ziggahunhow/91c88e162f00c608c6fb8fc8af1671cf to your computer and use it in GitHub Desktop.
The purpose of this script is to facilitate the drudging work of copying and pasting translation data in frontend applications
#! /bin/bash
# This script copies the first (json key) column and the user specified column
# of a csv file, appends it to an existing json file to create a new temp file.
# Caveat
# If you have the same key, jq will overwrite the value instead of appending it.
# Inputs:
# $1 = path of csv file to read from
# $2 = row number from the csv file to read from, starts from 1
# $3 = path of .json file to append to
# Output:
# .json file with the new translation appended to it, saved to $3.tmp
# ------------------------------
# Example input data table:
# +-------------+--------------------+-----------+
# | key | English | Chinese |
# +-------------+--------------------+-----------+
# | header | Cool App | 酷app |
# | description | This is a cool app | 這是個酷app|
# +-------------+--------------------+-----------+
# Example input csv format (data from the table above):
# key,English,Chinese
# header,Cool App,酷app
# description,This is a cool app,這是個酷app
# Run the script, This will copy the key and Chinese column from the csv file:
# ./cpTransaltions.sh ./coolApp.csv 2 ./zh.json
if [[ $1 =~ .*\.csv$ ]]
then
ret=$(
awk -F, -v var="$2" {'print $1 "=deliminator=" $var'} $1 |
awk -F'=deliminator=' '{ print " \""$1"\":\""$2"\","}' |
tail -n +2 |
tr '\n' ' ' |
sed 's/\(.*\),/\1 /' |
xargs -0 -I {} echo "{{}}"
)
tmpFile=$(echo $3 | sed 's/\(.*\)\.json/\1.tmp.json/')
jq --argjson var "$ret" '. += $var' $3 > $tmpFile &&
echo "written to $tmpFile"
else
echo "Please pass in a csv file path as the 1st arg" >&2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment