gpt4 book ai didi

linux - 将值从 bash for 循环转换为 json 对象

转载 作者:太空宇宙 更新时间:2023-11-04 09:31:09 24 4
gpt4 key购买 nike

下面是我对 txt 文件名进行排序的 for 循环片段。然后我试图将结果保存在 json 格式的文件中。但是,它会导致不符合要求的 json 格式。我如何将 for 循环中的值转换为所需的 json 格式?

dir="myfiles/test/"

prefix=""
echo "[" >> test.json
for dir in "${array[@]}"; do
#reverse the result of comparisons
file=$(find "$dir" -maxdepth 1 -type f -iname '*.txt' | awk "NR==$i")
[[ -n $file ]] &&
printf '%b{ "filepath": "%s" }' $prefix "$file" >> test.json
prefix=",\n"
done
echo
echo "]" >> test.json

当前输出

[
{ "filepath" : "myfiles/test/sdfsd.txt" },
{ "filepath" : "myfiles/test/piids.txt" },
{ "filepath" : "myfiles/test/saaad.txt" },
{ "filepath" : "myfiles/test/smmnu.txt" },
]

期望的输出

[
[
{ "filepath" : "myfiles/test/sdfsd.txt" }
],
[
{ "filepath" : "myfiles/test/piids.txt" }
],
[
{ "filepath" : "myfiles/test/saaad.txt" }
],
[
{ "filepath" : "myfiles/test/smmnu.txt" }
]
]

也允许

[
[
{ "filepath" : "myfiles/test/sdfsd.txt" },
{ "filepath" : "myfiles/test/sdfsd2.txt" }
],
[
{ "filepath" : "myfiles/test/piids.txt" },
{ "filepath" : "myfiles/test/piids2.txt" }
],
[
{ "filepath" : "myfiles/test/saaad.txt" }
],
[
{ "filepath" : "myfiles/test/smmnu.txt" }
]
]

最佳答案

结合使用jq 实现你的目标。首先,我们在句法上将不需要的输出转换为正确的格式。然后我们使用 jq 格式化它。

我们使用以下 awk 脚本:

{
# extract names of files (to see if they are equal
# besides a numerical suffix).
name1 = line
name2 = $0
sub(/"[^"]*$/, "", name1)
sub(/"[^"]*$/, "", name2)
sub(/.*\//, "", name1)
sub(/.*\//, "", name2)
sub(/\....$/, "", name1)
sub(/\....$/, "", name2)
sub(/[0-9]*$/, "", name1)
sub(/[0-9]*$/, "", name2)
# add array symbols to the line
# if last item was closed by a ']' add '[' to beginning
if (closed)
sub(/{/, "[{", line)
# if names are equal, same array
if (name1 != name2) {
sub(/},/, "}],", line)
closed = 1
} else
closed = ""
# if last line, consisting of simply a '['
if ($0 ~ /^]$/)
# remove extra comma at end of line
sub(/,$/, "", line)
# if line is set, print line
if (line)
print line
# set current line to line variable
line = $0
}

这会产生格式错误的输出:

$ cat file 
[
{ "filepath" : "myfiles/test/sdfsd.txt" },
{ "filepath" : "myfiles/test/piids.txt" },
{ "filepath" : "myfiles/test/saaad.txt" },
{ "filepath" : "myfiles/test/smmnu.txt" },
]
$ awk -f script.awk file
[
[{ "filepath" : "myfiles/test/sdfsd.txt" }],
[{ "filepath" : "myfiles/test/piids.txt" }],
[{ "filepath" : "myfiles/test/saaad.txt" }],
[{ "filepath" : "myfiles/test/smmnu.txt" }]
]

我们现在可以使用 jq 格式化:

$ awk -f script.awk file | jq .
[
[
{
"filepath": "myfiles/test/sdfsd.txt"
}
],
[
{
"filepath": "myfiles/test/piids.txt"
}
],
[
{
"filepath": "myfiles/test/saaad.txt"
}
],
[
{
"filepath": "myfiles/test/smmnu.txt"
}
]
]

请注意,这会处理几乎相同的文件,因为它们仅在数字后缀上有所不同。示例:

$ cat file 
[
{ "filepath" : "myfiles/test/sdfsd.txt" },
{ "filepath" : "myfiles/test/sdfsd2.txt" },
{ "filepath" : "myfiles/test/piids.txt" },
{ "filepath" : "myfiles/test/saaad.txt" },
{ "filepath" : "myfiles/test/smmnu.txt" },
]
$ awk -f script.awk file | jq .
[
[
{
"filepath": "myfiles/test/sdfsd.txt"
},
{
"filepath": "myfiles/test/sdfsd2.txt"
}
],
[
{
"filepath": "myfiles/test/piids.txt"
}
],
[
{
"filepath": "myfiles/test/saaad.txt"
}
],
[
{
"filepath": "myfiles/test/smmnu.txt"
}
]
]

关于linux - 将值从 bash for 循环转换为 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31226406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com