Last active
August 29, 2015 13:57
-
-
Save nvarun/9568270 to your computer and use it in GitHub Desktop.
[Fedora 20] Converting File having pipe-delimited values into JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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