我有这个脚本:
#!/bin/bash
# rsync using variables
echo 'Id,Name,Amount,TS,unique_ID' > lfs.csv
epoch=$(date +%s)
u_id=0
for (( id=1 ; ;id++ ))
do
[[ $u_id == 100 ]] && (( u_id = 1 )) || (( u_id +=1 ))
echo $id",Danny_"$id","$id","$epoch","$u_id >> lfs.csv
sleep
done
我希望能够插入一个代表JSON数据的列:
{
"totalMemory" : 12206567424,
"totalProcessors" : 4,
"version" : "0.4.1",
"startTime" : 1494851008421,
"agent" : {
"reconnectRetrySec" : 5,
"agentName" : "1002",
"checkRecovery" : false,
"backPressure" : 10000,
"throttler" : 100
},
"logPath" : "/eq/equalum/eqagent-0.4.1.0-SNAPSHOT/logs",
"status" : {
"current" : "active",
"currentMessage" : null,
"previous" : "pending",
"previousMessage" : "Recovery:Starting pipelines"
},
"autoStart" : false,
"recovery" : {
"agentName" : "1002",
"partitionInfo" : { },
"topicToInitialCapturePosition" : { }
},
"sources" : [ ],
"upTime" : "5 min, 26 sec",
"build" : "0-SNAPSHOT",
"target" : {
"targetType" : "equalum",
"agentID" : 1002,
"engineServers" : "<some_info>",
"kafkaOptions" : null,
"eventsServers" : "<some_info>",
"jaasConfigurationPath" : null,
"securityProtocol" : "PLAINTEXT",
"stateMonitorTopic" : "_state_change",
"targetType" : "equalum",
"status" : {
"current" : "active",
"currentMessage" : null,
"previous" : "pending",
"previousMessage" : "Recovery:Starting pipelines"
},
"serializationFormat" : "avroBinary"
}
}
我想添加另一个代表JSON列的列,它将包含上面的JSON字符串。 我想过如何添加列但我的主要目的是将JSOn字符串插入到我将其用作本地文件系统的列中。
答案 0 :(得分:0)
你可以用urlencode例如用这个函数写下另一个答案
url_encoder() { (
LC_ALL=C
for ((i=0;i<${#str};i+=1)); do
c=${str:i:1}
if [[ $c = [-_./a-zA-Z0-9] ]]; then
echo -n "${c}"
elif [[ $c = [$'\1'-$'\x7f'] ]]; then
printf '%%%02X' "'$c"
else
printf '%%%s' $(echo -n "$c" | od -An -tx1)
fi
done
)}
echo "$id,Danny_$id,$id,$epoch,$u_id,$(url_encoder "$your_data")" >> lfs.csv
或更有效率
# urlencode
perl -MURI::Escape -e 'print uri_escape shift' "$your_data"
# urldecode
perl -MURI::Escape -e 'print uri_unescape shift' "$your_data"