Хорошо, что предыдущий ответ 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
Некоторые могут комментировать, что с помощью этого документа вместо того, чтобы делать несколько эхо-заявлений, подобных этому, будут более эффективными, но я нахожу, что здесь docs испортили отступ, и я начал с исходного скрипта, это было быстрее, как это.
Вы можете задать true
для scheduler.Shutdown (), чтобы дождаться завершения работ. Это должно задержать отключение пула до конца выполнения заданий.
lifetime.ApplicationStopping.Register(() => scheduler.Shutdown(true));
Вы можете узнать больше об этом здесь .