JQ -c/--compact-output无法正常工作?杰森解析 [英] JQ -c / --compact-output not working properly? Json Parsing
问题描述
大约一个小时前,我刚刚在一个问题中向我介绍了JQ,但是我正在解析一个非常大的数据库,使用JQ时,输出看起来像这样:
I was just introduced to JQ in a question I posted about an hour ago, I'm parsing a very large database however, with JQ the output looks like this:
"removedforprivacy@gmail.com"
"john"
"smith"
null
null
"123 road st"
null
"Columbia"
"29203"
"SC"
null
相反,我希望它看起来像这样:
instead I want it to look like this:
"removedforprivacy@gmail.com"
"john"
"smith"
null
null
"123 road st"
null
"Columbia"
"29203"
"SC"
null
甚至更好:
"removedforprivacy@gmail.com","john","smith",null,null,"123,road,st",null,"Columbia","29203","SC",null
我当前正在使用此命令:
I'm currently using this command:
jq -c '(.email, .first_name, .last_name, .ip, .address, .address1, .address2, .city, .zip, .state, .phone)' file.json > file2.json
我也尝试过使用此命令:
I've tried using this command as well:
jq -compact-output '(.email, .first_name, .last_name, .ip, .address, .address1, .address2, .city, .zip, .state, .phone)' file.json > file2.json
但是file2.json仍然显示如下数据:
but file2.json still shows data like this:
"removedforprivacy@gmail.com"
"john"
"smith"
null
null
"123 road st"
null
"Columbia"
"29203"
"SC"
null
简而言之,我想将输出转换为看起来像csv或csv的内容,以便我可以更好地对其进行管理.
In short, I'd like to turn the output into something that looks like a csv or is a csv so I can manage it better.
该命令不起作用,只需要使用此命令一次即可.
The command isn't working and just need this command for a one time use.
推荐答案
如果要CSV,则当然可以简单地使用@csv
过滤器,但是@csv
会将null
转换为空字段:
If you want CSV then you could of course simply use the @csv
filter, but @csv
converts null
to an empty field:
"removedforprivacy@gmail.com","john","smith",,,"123 road st",,"Columbia","29203","SC",
要以似乎想要的方式处理字符串和null
,可以按以下方式将@tsv
与-r命令行选项结合使用:
To handle strings and null
the way you seem to want, you could use @tsv
as follows in conjunction with the -r command-line option:
map(if type == "string" then "\"\(.)\"" else "null" end)
| @tsv | gsub("\t";",")
使用您的输入,将产生:
With your input, this produces:
"removedforprivacy@gmail.com","john","smith",null,null,"123 road st",null,"Columbia","29203","SC",null
对于其他变体,您可能希望使用join/1
和/或-j命令行选项.
For other variations, you may wish to use join/1
and/or the -j command-line option.
如手册所述:
使用此选项可以通过将每个JSON对象放在同一行上来实现更紧凑的输出.
Using this option will result in more compact output by instead putting each JSON object on a single line.
此处"JSON对象"表示"JSON实体".
Here "JSON object" means "JSON entity".
这篇关于JQ -c/--compact-output无法正常工作?杰森解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!