我有一个.csv文件,其中有一个简短的变量列表,我需要从中创建一个.json文件。一切都工作得很好,但其中一个变量($input[5]/“programNumber”)需要作为整数导入,我使用的脚本是用一组引号提取所有内容,这对前4个变量都很好。我只是对导入语法不够熟悉,无法理解它。先谢谢你的帮助。
以下是脚本:
jq -Rsn '
{"services":
[inputs
| . / "\n"
| (.[] | select(length > 0) | . / ",") as $input
| {
"type": "mpeg2tsUdp",
"sources":
[
{
"address": $input[1],
"port": 2000,
"sourceAddresses":
[
$input[3]
],
"programSelection":
{
"type": "PmtProgramNumber",
"programNumber": $input[5]
}
},
{
"address": $input[2],
"port": 2000,
"sourceAddresses":
[
$input[4]
],
"programSelection":
{
"type": "PmtProgramNumber",
"programNumber": $input[5]
}
}
],
"streams": [],
"id": $input[0]
}
],
"id": "Inputs",
"name": "Inputs"
}
' <SourceList.csv
下面是.csv的内容:
FNCHD,239.194.208.9,239.194.216.9,10.110.208.9,10.110.216.9,23
MTV,239.194.208.216,239.194.216.216,10.110.208.216,10.110.216.216,702
AEHD,239.194.208.6,239.194.216.6,10.110.208.6,10.110.216.6,1
SLATE,239.1.1.1,239.1.2.1,10.100.1.1,10.100.2.1,3
这里是输出:
{
"services": [
{
"type": "mpeg2tsUdp",
"sources": [
{
"address": "239.194.208.9",
"port": 2000,
"sourceAddresses": [
"10.110.208.9"
],
"programSelection": {
"type": "PmtProgramNumber",
"programNumber": "23"
}
},
{
"address": "239.194.216.9",
"port": 2000,
"sourceAddresses": [
"10.110.216.9"
],
"programSelection": {
"type": "PmtProgramNumber",
"programNumber": "23"
}
}
],
"streams": [],
"id": "FNCHD"
},
{
"type": "mpeg2tsUdp",
"sources": [
{
"address": "239.194.208.216",
"port": 2000,
"sourceAddresses": [
"10.110.208.216"
],
"programSelection": {
"type": "PmtProgramNumber",
"programNumber": "702"
}
},
{
"address": "239.194.216.216",
"port": 2000,
"sourceAddresses": [
"10.110.216.216"
],
"programSelection": {
"type": "PmtProgramNumber",
"programNumber": "702"
}
}
],
"streams": [],
"id": "MTV"
},
{
"type": "mpeg2tsUdp",
"sources": [
{
"address": "239.194.208.6",
"port": 2000,
"sourceAddresses": [
"10.110.208.6"
],
"programSelection": {
"type": "PmtProgramNumber",
"programNumber": "1"
}
},
{
"address": "239.194.216.6",
"port": 2000,
"sourceAddresses": [
"10.110.216.6"
],
"programSelection": {
"type": "PmtProgramNumber",
"programNumber": "1"
}
}
],
"streams": [],
"id": "AEHD"
},
{
"type": "mpeg2tsUdp",
"sources": [
{
"address": "239.1.1.1",
"port": 2000,
"sourceAddresses": [
"10.100.1.1"
],
"programSelection": {
"type": "PmtProgramNumber",
"programNumber": "3"
}
},
{
"address": "239.1.2.1",
"port": 2000,
"sourceAddresses": [
"10.100.2.1"
],
"programSelection": {
"type": "PmtProgramNumber",
"programNumber": "3"
}
}
],
"streams": [],
"id": "SLATE"
}
],
"id": "Inputs",
"name": "Inputs"
}```
Anything I tried; including removing the quotes around \n and ",", either failed the script completely or changed nothing.
1条答案
按热度按时间68de4m5k1#
下面是使用
tonumber
一次的压缩版本: