"Product ID";"Product Name";"Price";"Description";
"1;"Example";"200";"Descripcion here...";
"2;"Example2";"300";"Some here...";
我想要这样的东西:
{
"title":"Example",
"price":200,
"description": {
"plain_text": "Some here..."
},
"Predifined":static content,
"Another Predifined":static content,
}
这是我尝试使用的脚本
我如何做到这一点?
答案 0 :(得分:3)
修正输入内容后,以分号正确分隔:
jq -R 'split(";")
| map(gsub("^\"|\"$";""))
| {title:.[1],
price: .[2],
description: { plain_text: .[3] }}' input.csv
产量:
{
"title": "Product Name",
"price": "Price",
"description": {
"plain_text": "Description"
}
}
{
"title": "Example",
"price": "200",
"description": {
"plain_text": "Descripcion here..."
}
}
{
"title": "Example2",
"price": "300",
"description": {
"plain_text": "Some here..."
}
}
答案 1 :(得分:1)
好吧,先前的jq
答案更加优雅。我仅使用bash进行了此操作...自从完成后,我还是把它放在这里了,但是在收到@peak的好回答后,我感到有点傻。
对于OP,第一个是免费的。对于将来的问题,此处的用户会帮助,但是您必须开始使用,并面对一个需要帮助;-)
的特定问题。#!/bin/bash
csvfile='input.csv'
# Make sure the file exists
if [ ! -f $csvfile ]
then
echo "ERROR: file $csvfile does not exist."
exit 1
fi
# Read the input file line per line
while read line
do
# Capture the information out of the line, fields separated by ;
IFS=';' read pid pname price desc <<< $line
# Output the JSON for this line
echo '{'
echo " \"title\":$pname,"
echo " \"price\":$price,"
echo " \"description\": {"
echo " \"plain_text\":$desc"
echo " },"
echo " \"Predifined\":\"static content\","
echo " \"Another Predifined\":\"static content\""
echo '}'
# Just to split the line's output
echo ""
done <$csvfile
哪个产生以下输出:
{
"title":"Product Name",
"price":"Price",
"description": {
"plain_text":"Description"
},
"Predifined":"static content",
"Another Predifined":"static content"
}
{
"title":"Example",
"price":"200",
"description": {
"plain_text":"Descripcion here..."
},
"Predifined":"static content",
"Another Predifined":"static content"
}
{
"title":"Example2",
"price":"300",
"description": {
"plain_text":"Some here..."
},
"Predifined":"static content",
"Another Predifined":"static content"
}
同一脚本的第二版,该脚本会将每个产品部分输出到其自己的JSON文件中。请注意,我删除了CSV文件的第一行,因为它只是标题,并且我不想为标题创建文件。
#!/bin/bash
csvfile='input.csv'
# Make sure the file exists
if [ ! -f $csvfile ]
then
echo "ERROR: file $csvfile does not exist."
exit 1
fi
# Read the input file line per line
sed '1d' $csvfile | while read line
do
# Capture the information out of the line, fields separated by ;
IFS=';' read pid pname price desc <<< $line
# Strip the " from $pid and define the output filename
output_filename="$(echo $pid | tr -d '"').json"
# Output the JSON for this line
echo '{' >$output_filename
echo " \"title\":$pname," >>$output_filename
echo " \"price\":$price," >>$output_filename
echo " \"description\": {" >>$output_filename
echo " \"plain_text\":$desc" >>$output_filename
echo " }," >>$output_filename
echo " \"Predifined\":\"static content\"," >>$output_filename
echo " \"Another Predifined\":\"static content\"" >>$output_filename
echo '}' >>$output_filename
# Just to split the line output
echo "" >>$output_filename
done
有人可能会评论说,使用此处文档而不是像这样执行多个echo语句会更有效,但是我发现此处文档使缩进变得混乱,并且我从原始脚本开始,它的速度更快。 / p>