我在名为data.txt
的文本文件中有数据,例如
1. John (1994) 92
2. Granny (1972) 82
我想使用Awk将此数据转换为JSON格式。预期结果:
[{
"ID" : "1",
"Name" : "John",
"Birth" : "1994",
"Marks" : "92"
}]
我用jq尝试过
jq -R '[ split("\n")[] | select(length > 0) | split(" ") | {ID: .[0], Name: .[1], Birth: .[2], Marks: .[3]}]' data.txt
答案 0 :(得分:2)
input_img = Input(shape=(150, 360, 3))
x = ZeroPadding2D(padding=(1, 0))(input_img)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
或者,您也可以执行以下操作。
awk ' BEGIN { print "[" ; } { print " {\n" " \"ID\" : \"" $1 "\",\n" " \"Name\" : \"" $2 "\",\n" " \"Birth\" : \"" $3 "\",\n" " \"Marks\" : \"" $4 "\"\n" " }" } END { print "]" } ' data.txt
然后您将看到以下输出。
awk ' BEGIN { print "[" ; } \
{ print " {" \
print " \"ID\" : \"" $1 "\"," \
print " \"Name\" : \"" $2 "\"," \
print " \"Birth\" : \"" $3 "\"," \
print " \"Marks\" : \"" $4 "\"" \
print " }" \
} \
END { print "]" } ' data.txt
答案 1 :(得分:0)
如果您对如何使用jq
感到好奇,这是一种方法。
parse.jq
split("\n") |
map(match("(\\d+)\\. +([\\w ]+) +\\((\\d+)\\) +(\\d+)")) | .[] |
{
"ID" : (.captures[0].string),
"Name" : (.captures[1].string),
"Birth" : (.captures[2].string),
"Marks" : (.captures[3].string)
}
像这样运行它:
jq -R -f parse.jq infile.txt
输出:
{
"ID": "1",
"Name": "John",
"Birth": "1994",
"Marks": "92"
}
{
"ID": "2",
"Name": "Granny",
"Birth": "1972",
"Marks": "82"
}