我有一個這樣的.file并想將其轉換為 JSON 格式。
2022/01/22 21:27:56 [notice] 40#40: signal process started
2022/01/22 21:27:56 [error] 40#40: open() "/run/nginx.pid" failed (2: No such file or directory)
2022/01/22 21:28:04 [notice] 42#42: signal process started
2022/01/22 21:28:04 [error] 42#42: open() "/run/nginx.pid" failed (2: No such file or directory)
代碼
#!/bin/bash
json=''
while read -r line; do
dt=$(echo ${line} | awk '{print ($1" "$2)}')
info=$(echo ${line} | awk '{print ($3)}')
error=$(echo ${line} | awk '{print ($4$5$6$7$8$9$10)}')
json ="{\"date\":\"$dt\","\"info\":\"$info\",\"error\":\"$error\""}"
done < "$file"
res="$json"
如何使 var res像帶有分隔符逗號 "," 每個物件 {} 的 JSON 格式
我想做這樣的輸出
[{"date":"2022/01/22 21:27:56","info":"[notice]","error":"40#40:signalprocessstarted"},
{"date":"2022/01/22 21:27:56","info":"[error]","error":"40#40:open()"/run/nginx.pid"failed(2:Nosuch"},
{"date":"2022/01/22 21:28:04","info":"[notice]","error":"42#42:signalprocessstarted"},
{"date":"2022/01/22 21:28:04","info":"[error]","error":"42#42:open()"/run/nginx.pid"failed(2:Nosuch"}]
但現在我的輸出看起來像這樣
{"date":"2022/01/22 21:27:56","info":"[notice]","error":"40#40:signalprocessstarted"}{"date":"2022/01/22 21:27:56","info":"[error]","error":"40#40:open()"/run/nginx.pid"failed(2:Nosuch"}{"date":"2022/01/22 21:28:04","info":"[notice]","error":"42#42:signalprocessstarted"}{"date":"2022/01/22 21:28:04","info":"[error]","error":"42#42:open()"/run/nginx.pid"failed(2:Nosuch"}
物件之間沒有任何分隔符
以及如何在 JSON中處理像“/run/nginx.pid”這樣的字串
uj5u.com熱心網友回復:
請您嘗試以下方法:
#!/bin/bash
echo -n "["
while read -r day hms info err; do # split on blank characters up to 4 words
if (( nr )); then # count the line number
echo "," # append a comma after the previous line
fi
err="$(sed -E 's/"/\\&/g' <<< "$err")" # escape double quotes in the message
printf '{"date":"%s %s","info":"%s","error":"%s"}' "$day" "$hms" "$info" "$err"
done < file
echo "]"
輸出:
[{"date":"2022/01/22 21:27:56","info":"[notice]","error":"40#40: signal process started"},
{"date":"2022/01/22 21:27:56","info":"[error]","error":"40#40: open() \"/run/nginx.pid\" failed (2: No such file or directory)"},
{"date":"2022/01/22 21:28:04","info":"[notice]","error":"42#42: signal process started"},
{"date":"2022/01/22 21:28:04","info":"[error]","error":"42#42: open() \"/run/nginx.pid\" failed (2: No such file or directory)"}]
[編輯]
如果你想為var生成的 json 格式的字串分配一個變數(例如,),請將整個代碼用$(and括起來),command substitution然后將變數分配給它,例如:
var=$(
echo -n "["
while read -r day hms info err; do # split on blank characters up to 4 words
if (( nr )); then # count the line number
echo "," # append a comma after the previous line
fi
err="$(sed -E 's/"/\\&/g' <<< "$err")" # escape double quotes in the message
printf '{"date":"%s %s","info":"%s","error":"%s"}' "$day" "$hms" "$info" "$err"
done < file
echo "]"
)
echo "$var" # just to see the result
如果jq可用,您還可以說:
echo -n "["
while read -r day hms info err; do # split on blank characters up to 4 words
if (( nr )); then # count the line number
echo "," # append a comma after the previous line
fi
jq -cn --arg date "$day $hms" --arg info "$info" --arg error "$err" '{date:$date, info:$info, error:$error}'
done < file
echo "]"
正如戈登戴維森所建議的那樣。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/419431.html
標籤:
