Skip to content

Instantly share code, notes, and snippets.

@nvarun
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nvarun/9568270 to your computer and use it in GitHub Desktop.
Save nvarun/9568270 to your computer and use it in GitHub Desktop.
[Fedora 20] Converting File having pipe-delimited values into JSON
#!/bin/ksh
test $# -lt 1 || test $# -gt 2 || test ! -f $1 && exit 1
cp $1 $1.o
echo -e "\t\"${2:-feeds}\": {"
while IFS='|' read iputKey iputRecord
do
echo -e "\t\t\"${iputKey}\": ["
echo $iputRecord | sed "s/\([^|]*\)\(|*\)/\t\t\t\"\1\",\n/g" | sed '/^$/d' | sed '$s/,$//'
sed '1d' $1.o > $1.n
mv $1.n $1.o
if [[ -s $1.o ]]; then
echo -e "\t\t],"
else
echo -e "\t\t]"
fi
done < $1
echo -e "\t}"
rm -f $1.*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment