管道卷曲输出,在 Ruby 中以 grep 结尾 o Txt 文件



im 在 Ruby 中使用 curl 时遇到问题。我想运行一个 ruby 脚本,它应该存储这样的 JSON 类型对象

{
"remote":  [
{"name": "cast-1",
"id": 1212
},
{"name": "cast-1",
"id": 1214
},
{"name": "home-11",
"id": 3212
},
{"name": "cast-3",
"id": 3212
},
{"name": "cast-3",
"id": 3213
},
{"name": "cast-4",
"id": 4211
}
]
}

在文本文件中。我映射并整理了卷曲输入以获得所需的条目。 现在,当我运行脚本时,我的 txt 文件弹出为空。 我尝试过的例子

#ruby
`curl -m 10 -s -H '[...]' "https://example_with_json_file.com" | jq -r .remote | group_by(.name) | map({ (.[0].name): map(.id) }) | "(.[])" | grep '3|4|11' > file.txt` 
foo = []
File.open(file.txt) do |file|
foo = file.readlines
end
[...]

在我的 temrinal 作品中运行上述输入。我尝试在我的 ruby 示例中使用 Kernel.system 订单系统。什么都没用。

如何将此输出通过管道传输到 txt 文件

{"cast-3":[3212,3213]}
{"cast-4":[4211]}
{"home-11":[3212]}

或者一般来说,是否可以在一行中运行 curl 输入,即使用 ruby 可执行?

总是最好用一种语言做事。喜欢:

response = HTTParty.get('https://path/to/json')
JSON.parse(response)
...

如果你想把它和Bash混合,试试:

bar = `curl -m 10 -s -H '[...]' "https://example_with_json_file.com" | jq -r .remote | group_by(.name) | map({ (.[0].name): map(.id) }) | "(.[])" | grep '3|4|11'`
foo = bar.split(/n/)
File.write('/path/to/file', bar)

最新更新