私は現在、自動採点機にコードを送信して結果を返すクラスを開発しています。返される形式は視覚的に解析するのが少し難しいので、パイプラインで使用できるスクリプトを作成して読みやすくしたいと思いました。
自動採点機の結果は次のとおりです。
Problem,Correct?,Correct Answer,Agent's Answer
"Challenge Problem B-04",0,4,-1
"Basic Problem B-12",0,1,-1
"Challenge Problem B-05",0,6,-1
"Challenge Problem B-07",0,6,-1
"Challenge Problem B-06",0,3,-1
"Basic Problem B-11",0,1,-1
"Basic Problem B-10",0,3,-1
"Challenge Problem B-03",0,3,-1
"Challenge Problem B-02",0,1,-1
"Challenge Problem B-01",0,6,-1
"Challenge Problem B-09",0,4,-1
"Challenge Problem B-08",0,4,-1
"Basic Problem B-08",0,6,-1
"Basic Problem B-09",0,5,-1
"Basic Problem B-04",0,3,-1
"Basic Problem B-05",0,4,-1
"Basic Problem B-06",0,5,-1
"Basic Problem B-07",0,6,-1
"Basic Problem B-01",0,2,-1
"Basic Problem B-02",0,5,-1
"Basic Problem B-03",0,1,-1
"Challenge Problem B-10",0,4,-1
"Challenge Problem B-11",0,5,-1
"Challenge Problem B-12",0,1,-1
{
"Basic Problems B": {
"Incorrect": "0",
"Skipped": "12",
"Correct": "0",
"Set": "Basic Problems B"
},
"Challenge Problems B": {
"Incorrect": "0",
"Skipped": "12",
"Correct": "0",
"Set": "Challenge Problems B"
}
}
コンマで区切られた値とJSONが混在しています。このすべてを私が読める素敵なテーブルに入れるのが良いでしょう。
現在私は次のようなものを持っています
python submit.py --provider gt --assignment error-check | column -t -s, | less -S
任意の出力:
{
"Basic Problems B": {
"Incorrect": "0",
"Skipped": "12",
"Correct": "0",
"Set": "Basic Problems B"
},
"Challenge Problems B": {
"Incorrect": "0",
"Skipped": "12",
"Correct": "0",
"Set": "Challenge Problems B"
}
}
Problem Correct? Correct Answer Agent's Answer
"Challenge Problem B-04" 0 4 -1
"Basic Problem B-12" 0 1 -1
"Challenge Problem B-05" 0 6 -1
"Challenge Problem B-07" 0 6 -1
"Challenge Problem B-06" 0 3 -1
"Basic Problem B-11" 0 1 -1
"Basic Problem B-10" 0 3 -1
"Challenge Problem B-03" 0 3 -1
"Challenge Problem B-02" 0 1 -1
"Challenge Problem B-01" 0 6 -1
"Challenge Problem B-09" 0 4 -1
"Challenge Problem B-08" 0 4 -1
"Basic Problem B-08" 0 6 -1
"Basic Problem B-09" 0 5 -1
"Basic Problem B-04" 0 3 -1
"Basic Problem B-05" 0 4 -1
"Basic Problem B-06" 0 5 -1
"Basic Problem B-07" 0 6 -1
"Basic Problem B-01" 0 2 -1
"Basic Problem B-02" 0 5 -1
"Basic Problem B-03" 0 1 -1
"Challenge Problem B-10" 0 4 -1
"Challenge Problem B-11" 0 5 -1
"Challenge Problem B-12" 0 1 -1
それは私をほとんどの時間そこにいます。これでJSONを処理する方法があるかどうか疑問に思います。
特定の行番号で出力を分割することはできませんが、最初は見つかりそうです{
。
できるだけ少ないことでクラスメートと共有したいと思いました。したがって、依存度が低いほど良いです。
外部コードの使用を提案する他のJSON解析ポストを見たことがあります。
理想的な出力は次のとおりです。
Problem Correct? Correct Answer Agent's Answer
"Challenge Problem B-04" 0 4 -1
"Basic Problem B-12" 0 1 -1
"Challenge Problem B-05" 0 6 -1
"Challenge Problem B-07" 0 6 -1
"Challenge Problem B-06" 0 3 -1
"Basic Problem B-11" 0 1 -1
"Basic Problem B-10" 0 3 -1
"Challenge Problem B-03" 0 3 -1
"Challenge Problem B-02" 0 1 -1
"Challenge Problem B-01" 0 6 -1
"Challenge Problem B-09" 0 4 -1
"Challenge Problem B-08" 0 4 -1
"Basic Problem B-08" 0 6 -1
"Basic Problem B-09" 0 5 -1
"Basic Problem B-04" 0 3 -1
"Basic Problem B-05" 0 4 -1
"Basic Problem B-06" 0 5 -1
"Basic Problem B-07" 0 6 -1
"Basic Problem B-01" 0 2 -1
"Basic Problem B-02" 0 5 -1
"Basic Problem B-03" 0 1 -1
"Challenge Problem B-10" 0 4 -1
"Challenge Problem B-11" 0 5 -1
"Challenge Problem B-12" 0 1 -1
Set Incorrect Skipped Correct
Basic Problems B 0 12 0
Challenge Problems B 0 12 0
答え1
JSONを残りの部分から分離するのは非常に簡単です。これはJSON以外のものを提供します。
python submit.py --provider gt --assignment error-check | sed '/{/,$d'
そしてこれはJSONだけです。
python submit.py --provider gt --assignment error-check | sed -n '/{/,$p'
これを説明するために、サンプル入力を次のように保存しましたfile
。
$ sed '/{/,$d' file
Problem,Correct?,Correct Answer,Agent's Answer
"Challenge Problem B-04",0,4,-1
"Basic Problem B-12",0,1,-1
"Challenge Problem B-05",0,6,-1
"Challenge Problem B-07",0,6,-1
"Challenge Problem B-06",0,3,-1
"Basic Problem B-11",0,1,-1
"Basic Problem B-10",0,3,-1
"Challenge Problem B-03",0,3,-1
"Challenge Problem B-02",0,1,-1
"Challenge Problem B-01",0,6,-1
"Challenge Problem B-09",0,4,-1
"Challenge Problem B-08",0,4,-1
"Basic Problem B-08",0,6,-1
"Basic Problem B-09",0,5,-1
"Basic Problem B-04",0,3,-1
"Basic Problem B-05",0,4,-1
"Basic Problem B-06",0,5,-1
"Basic Problem B-07",0,6,-1
"Basic Problem B-01",0,2,-1
"Basic Problem B-02",0,5,-1
"Basic Problem B-03",0,1,-1
"Challenge Problem B-10",0,4,-1
"Challenge Problem B-11",0,5,-1
"Challenge Problem B-12",0,1,-1
そして
$ sed -n '/{/,$p' file
{
"Basic Problems B": {
"Incorrect": "0",
"Skipped": "12",
"Correct": "0",
"Set": "Basic Problems B"
},
"Challenge Problems B": {
"Incorrect": "0",
"Skipped": "12",
"Correct": "0",
"Set": "Challenge Problems B"
}
}
今、私は非JSONをすでにうまく処理しているので、変更しないでください。理想的には、JSONデータを解析するためにJSONパーサーを使用する必要があります(たとえば、少なくとも必要な操作を実行します(コマンドで置き換えます)jq
。jq
cat file
python submit.py --provider gt --assignment error-check
$ cat file | sed -n 's/[,"]//g; s/^ *//; /{/,$p' | tac | awk -F': ' 'BEGIN{printf "%-30s%-10s%-10s%-10s\n", "Set", "Incorrect", "Skipped", "Correct"} NF==2 && !/\{/{if($1=="Set"){set=$2;data[set]["Incorrect"] = 0;data[set]["Skipped"] = 0;data[set]["Correct"] = 0;} data[set][$1]=$2}END{for(set in data){printf "%-30s%-10s%-10s%-10s\n", set,data[set]["Incorrect"],data[set]["Skipped"],data[set]["Correct"]}}'
Set Incorrect Skipped Correct
Challenge Problems B 0 12 0
Basic Problems B 0 12 0
これらすべてをシェルスクリプトに入れると、次のようになります。
#!/bin/bash
tmpFile=$(mktemp)
python submit.py --provider gt --assignment error-check > "$tmpFile";
sed '/{/,$d' "$tmpFile" | column -t -s,
sed -n 's/[,"]//g; s/^ *//; /{/,$p' "$tmpFile" |
tac |
awk -F': ' '
BEGIN{
printf "%-30s%-10s%-10s%-10s\n", "Set", "Incorrect", "Skipped", "Correct"
}
NF==2 && !/\{/{
if($1=="Set"){
set=$2;
data[set]["Incorrect"] = 0;
data[set]["Skipped"] = 0;
data[set]["Correct"] = 0;
}
data[set][$1]=$2
}
END{
for(set in data){
printf "%-30s%-10s%-10s%-10s\n", set,
data[set]["Incorrect"],
data[set]["Skipped"],
data[set]["Correct"]}
}'
rm "$tmpFile"
次の出力が生成されます。
$ foo.sh
Problem Correct? Correct Answer Agent's Answer
"Challenge Problem B-04" 0 4 -1
"Basic Problem B-12" 0 1 -1
"Challenge Problem B-05" 0 6 -1
"Challenge Problem B-07" 0 6 -1
"Challenge Problem B-06" 0 3 -1
"Basic Problem B-11" 0 1 -1
"Basic Problem B-10" 0 3 -1
"Challenge Problem B-03" 0 3 -1
"Challenge Problem B-02" 0 1 -1
"Challenge Problem B-01" 0 6 -1
"Challenge Problem B-09" 0 4 -1
"Challenge Problem B-08" 0 4 -1
"Basic Problem B-08" 0 6 -1
"Basic Problem B-09" 0 5 -1
"Basic Problem B-04" 0 3 -1
"Basic Problem B-05" 0 4 -1
"Basic Problem B-06" 0 5 -1
"Basic Problem B-07" 0 6 -1
"Basic Problem B-01" 0 2 -1
"Basic Problem B-02" 0 5 -1
"Basic Problem B-03" 0 1 -1
"Challenge Problem B-10" 0 4 -1
"Challenge Problem B-11" 0 5 -1
"Challenge Problem B-12" 0 1 -1
Set Incorrect Skipped Correct
Challenge Problems B 0 12 0
Basic Problems B 0 12 0
ハッキングされているように感じても、誰かが専用のJSONパーサーを使用してよりきれいな解決策を思い付くことを願っています。
スチールドライバーjq
コメントに正しい解決策が提供されてうれしいので、それを含めるとより簡単で安全な解決策が得られます。
#!/bin/bash
tmpFile=$(mktemp)
python submit.py --provider gt --assignment error-check > "$tmpFile";
sed '/{/,$d' "$tmpFile" | column -t -s,
sed -n '/{/,$p' "$tmpFile" |
jq -r '["Set","Incorrect","Skipped","Correct"], (.[] | [.Set,.Incorrect,.Skipped,.Correct]) | @tsv'
rm "$tmpFile"
答え2
ミラーの使用(https://github.com/johnkerl/miller)と実行
# get the CSV and transform it into a pretty print table
<input grep -P '^("|\w)' | mlr --c2p cat >out
# add a carriage return
echo "" >> out
# convert the json into a pretty print table and add it to the output
<input grep -vP '^("|\w)' | mlr --j2p cat -n then reshape -r "(Basi|Chal)" -o i,v \
then nest --explode --values --across-fields --nested-fs ":" -f i \
then reshape -s i_2,v \
then cut -x -f i_1,n \
then reorder -f Set >>out
あなたはやる
Problem Correct? Correct Answer Agent's Answer
Challenge Problem B-04 0 4 -1
Basic Problem B-12 0 1 -1
Challenge Problem B-05 0 6 -1
Challenge Problem B-07 0 6 -1
Challenge Problem B-06 0 3 -1
Basic Problem B-11 0 1 -1
Basic Problem B-10 0 3 -1
Challenge Problem B-03 0 3 -1
Challenge Problem B-02 0 1 -1
Challenge Problem B-01 0 6 -1
Challenge Problem B-09 0 4 -1
Challenge Problem B-08 0 4 -1
Basic Problem B-08 0 6 -1
Basic Problem B-09 0 5 -1
Basic Problem B-04 0 3 -1
Basic Problem B-05 0 4 -1
Basic Problem B-06 0 5 -1
Basic Problem B-07 0 6 -1
Basic Problem B-01 0 2 -1
Basic Problem B-02 0 5 -1
Basic Problem B-03 0 1 -1
Challenge Problem B-10 0 4 -1
Challenge Problem B-11 0 5 -1
Challenge Problem B-12 0 1 -1
Set Incorrect Skipped Correct
Basic Problems B 0 12 0
Challenge Problems B 0 12 0
答え3
$ cat tst.awk
BEGIN { FS=","; OFS="\t" }
/{/ { FS="(^|\":)[[:space:]]+\"|\",?" }
FS == "," { $1=$1; print; next }
{ f[$2] = $3 }
/}/ {
if ( !doneHdr++ ) {
print "Set", "Incorrect", "Skipped", "Correct"
}
print f["Set"], f["Incorrect"], f["Skipped"], f["Correct"]
}
。
$ awk -f tst.awk file | column -s$'\t' -t
Problem Correct? Correct Answer Agent's Answer
"Challenge Problem B-04" 0 4 -1
"Basic Problem B-12" 0 1 -1
"Challenge Problem B-05" 0 6 -1
"Challenge Problem B-07" 0 6 -1
"Challenge Problem B-06" 0 3 -1
"Basic Problem B-11" 0 1 -1
"Basic Problem B-10" 0 3 -1
"Challenge Problem B-03" 0 3 -1
"Challenge Problem B-02" 0 1 -1
"Challenge Problem B-01" 0 6 -1
"Challenge Problem B-09" 0 4 -1
"Challenge Problem B-08" 0 4 -1
"Basic Problem B-08" 0 6 -1
"Basic Problem B-09" 0 5 -1
"Basic Problem B-04" 0 3 -1
"Basic Problem B-05" 0 4 -1
"Basic Problem B-06" 0 5 -1
"Basic Problem B-07" 0 6 -1
"Basic Problem B-01" 0 2 -1
"Basic Problem B-02" 0 5 -1
"Basic Problem B-03" 0 1 -1
"Challenge Problem B-10" 0 4 -1
"Challenge Problem B-11" 0 5 -1
"Challenge Problem B-12" 0 1 -1
Set Incorrect Skipped Correct
Basic Problems B 0 12 0
Challenge Problems B 0 12 0
Challenge Problems B 0 12 0