如何将JQ中数组中的单个元素传递给xargs
How to pass individual elements in an array in JQ to xargs
我有一个这样的数组:
[
{
"customer": {
"id": "61ae22891dbcb5200f4af298",
"customerId": "C0001346",
"companyName": "Real Constructions",
"displayName": "Real Constructions",
"fullName": "NiyatiU",
"email": "niyati@curbwaste.com",
"phone": "(987) 654-3209"
},
"address": {
"streetName": "NY",
"state": "NY",
"city": "NY",
"zipcode": "39092"
}
},
{
"customer": {
"id": "61ae29f31dbcb5200f4af2b2",
"customerId": "C0001347",
"companyName": "UK Reality",
"displayName": "UK Reality",
"fullName": "VatsalShah",
"email": "vatsal@curbwaste.com",
"phone": "(766) 688-1348"
},
"address": {
"streetName": "Queens",
"state": "NY",
"city": "Queens",
"zipcode": "11354"
}
}
]
我想将每个对象作为单独的参数传递给 xargs 和 curl。伪代码如
jq '. | map(@json) | join("\0")' http/customer/us/customers.json \
| xargs -0 -n 1 -I % curl -X POST http://localhost:3000/customers -d '%'
但后来我得到:
xargs: insufficient space for argument
因为 json 元素太大,我想。我不想写一堆文件。
jq -j 'map(@json) | join("\u0000")' http/customer/us/customers.json |
xargs -0 -I % curl -X POST http://localhost:3000/customers -d %
因为 @json
从不产生任何换行符,以下也可以工作:
jq -r '.[] | @json' http/customer/us/customers.json |
xargs -I % curl -X POST http://localhost:3000/customers -d %
Demo 在 jq play
--join-output
/-j
打印字符串 as-is 而不是打印生成字符串的 JSON 字符串文字。不添加任何换行符,甚至不添加尾随符。
--raw-output
/-r
打印字符串 as-is 而不是打印生成字符串的 JSON 字符串文字。在每个字符串后添加一个换行符。
"\0"
不会产生 NUL。为此,您需要 "\u0000"
。
. |
没用
'%'
和%
没有区别。他们都将 %
传递给 xargs
.
-n 1
没用。 -I
意味着 -L 1
,这使 -n 1
变得多余。
{}
比 %
更常用作替换表达式。但是 %
也可以,所以我没有更改它。
使用--join-output
(或-j
)选项输出没有换行符的原始文本,并使用\u0000
创建NUL
字符:
jq -j 'map(@json) | join("\u0000")' http/customer/us/customers.json \
| xargs -0 -n 1 -I % curl -X POST http://localhost:3000/customers -d '%'
我有一个这样的数组:
[
{
"customer": {
"id": "61ae22891dbcb5200f4af298",
"customerId": "C0001346",
"companyName": "Real Constructions",
"displayName": "Real Constructions",
"fullName": "NiyatiU",
"email": "niyati@curbwaste.com",
"phone": "(987) 654-3209"
},
"address": {
"streetName": "NY",
"state": "NY",
"city": "NY",
"zipcode": "39092"
}
},
{
"customer": {
"id": "61ae29f31dbcb5200f4af2b2",
"customerId": "C0001347",
"companyName": "UK Reality",
"displayName": "UK Reality",
"fullName": "VatsalShah",
"email": "vatsal@curbwaste.com",
"phone": "(766) 688-1348"
},
"address": {
"streetName": "Queens",
"state": "NY",
"city": "Queens",
"zipcode": "11354"
}
}
]
我想将每个对象作为单独的参数传递给 xargs 和 curl。伪代码如
jq '. | map(@json) | join("\0")' http/customer/us/customers.json \
| xargs -0 -n 1 -I % curl -X POST http://localhost:3000/customers -d '%'
但后来我得到:
xargs: insufficient space for argument
因为 json 元素太大,我想。我不想写一堆文件。
jq -j 'map(@json) | join("\u0000")' http/customer/us/customers.json |
xargs -0 -I % curl -X POST http://localhost:3000/customers -d %
因为 @json
从不产生任何换行符,以下也可以工作:
jq -r '.[] | @json' http/customer/us/customers.json |
xargs -I % curl -X POST http://localhost:3000/customers -d %
Demo 在 jq play
--join-output
/-j
打印字符串 as-is 而不是打印生成字符串的 JSON 字符串文字。不添加任何换行符,甚至不添加尾随符。--raw-output
/-r
打印字符串 as-is 而不是打印生成字符串的 JSON 字符串文字。在每个字符串后添加一个换行符。"\0"
不会产生 NUL。为此,您需要"\u0000"
。. |
没用'%'
和%
没有区别。他们都将%
传递给xargs
.-n 1
没用。-I
意味着-L 1
,这使-n 1
变得多余。{}
比%
更常用作替换表达式。但是%
也可以,所以我没有更改它。
使用--join-output
(或-j
)选项输出没有换行符的原始文本,并使用\u0000
创建NUL
字符:
jq -j 'map(@json) | join("\u0000")' http/customer/us/customers.json \
| xargs -0 -n 1 -I % curl -X POST http://localhost:3000/customers -d '%'