我有一个脚本,当我得到值时,我将以json格式输出它们。
以下是我的剧本:
#!/bin/bash
if
[ "$(egrep -l 'TR.834' /home/user/for_test_dir/*) " ];
then
egrep -l 'TR.834' /home/user/for_test_dir/* > /tmp/outbound.txt
chmod 777 /tmp/result.txt
#json_File=~/outboundFile.json
info_template='
{
"date" : "%s",
"time" : "%s",
"type" : "%s",
"num" : "%s"
},\n'
echo '{' #>> $json_File
echo -e '\t"List": {' #>> $json_File
echo -e '\t\t"Data" : [' #>> $json_File
a=1
while read LINE
do
fullpath[$a]=$LINE
filename[$a]=`basename $LINE`
cat $LINE | awk '{gsub("-",RS);print}' > /tmp/temp2.txt
chmod 777 /tmp/temp2.txt
cat /tmp/temp2.txt | awk '{gsub("*",RS);print}' > /tmp/temp.txt
chmod 777 /tmp/temp.txt
DATE[$a]=`sed -n '10p' < /tmp/temp.txt`
TIME[$a]=`sed -n '11p' < /tmp/temp.txt`
TYPE[$a]=`sed -n '28p' < /tmp/temp.txt`
NUM[$a]=`grep -oP 'BEN.\K[A-Z0-9_-\s]*' < /tmp/temp2.txt | awk '{ printf "%s,", $0 }'| sed 's/,\+$//' | awk '!x[$0]++'`
printf "$template" "${DATE[$a]}"\
"${TIME[$a]}"\
"${TYPE[$a]}"\
"${num[$a]}" #>> $json_File
done < /tmp/result.txt
fi
echo -e "\t\t]" #>> $json_File
echo -e "\t}" #>> $json_File
echo -e "}" #>> $json_File
我的问题是当我提取多个数据时。例如,&#34; NUM&#34;:&#34; 3232&#34;,&#34; 232343&#34;,&#34; 23345&#34;, 我对此的预期输出将是这样的:
"NUM" : [
"3232",
"232343",
"23345"
]
对于单一数据,&#34; NUM&#34; :&#34; 23234&#34; (格式将按原样)。非常需要你的帮助。
答案 0 :(得分:0)
echo '{ "NUM": "3232", "232343", "23345" }' | python -m json.tool
或
sudo gem install json
echo '{ "NUM": "3232", "232343", "23345" }' | prettify_json.rb