Skip to content

Instantly share code, notes, and snippets.

@waltervargas
Last active October 27, 2018 01:12
Show Gist options
  • Save waltervargas/ff26fea95cf87093a6534f7bddf8cc1b to your computer and use it in GitHub Desktop.
Save waltervargas/ff26fea95cf87093a6534f7bddf8cc1b to your computer and use it in GitHub Desktop.
csv to json in bash
#!/bin/sh
_csv () {
cat /dev/stdin <<EOF
"text/html","gzip","en;q=0.5","close","httpbin.org","1","curl","20"
"text/html","gzip","en;q=0.5","close","httpbin.org","1","curl","20"
"text/html","gzip","en;q=0.5","close","httpbin.org","1","curl","20"
"text/html","gzip","en;q=0.5","close","httpbin.org","1","curl","20"
"text/html","gzip","en;q=0.5","close","httpbin.org","1","curl","20"
EOF
}
_json () {
cat /dev/stdin <<EOF
{
"headers": {
"Accept": "${Accept}",
"Accept-Encoding": "${AcceptEncoding}",
"Accept-Language": "${AcceptLanguage}",
"Connection": "${Connection}",
"Host": "${Host}",
"Upgrade-Insecure-Requests": "${UpgradeInsecureRequests}",
"User-Agent": "${UserAgent}",
"X-Imforwards": "${XImforwards}"
}
}
EOF
}
_csv_to_json () {
(IFS=,
read Accept \
AcceptEncoding \
AcceptLanguage \
Connection \
Host \
UpgradeInsecureRequests \
UserAgent \
XImforwards
_json
) <<< "${1}"
}
while read row; do
_csv_to_json "${row//\"/}"
done <<< "$(_csv)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment