Column is an interesting command – it will turn delimited text into, well, columns. Simply tell it you want a table (-t) and indicate what separator to use (-s). Optionally, you can add table column headers
[lisa@linux01 ~/]# cat /etc/group | column -t -s :
root x 0 root,lisa
bin x 1
daemon x 2
...
passim x 987
Alternately, you can use -J to get JSON-formatted output. Here you need the –table-columns as a comma delimited list of column names:
[lisa@linux01 ~/]# cat /etc/group | column -J -s : --table-columns "group,password,gid,members"
{
"table": [
{
"group": "root",
"password": "x",
"gid": "0",
"members": "root,lisa"
},{
"group": "bin",
"password": "x",
"gid": "1",
"members": null
},{
"group": "daemon",
"password": "x",
"gid": "2",
"members": null
},{
"group": "passim",
"password": "x",
"gid": "987",
"members": null
}
]
}
Which can then be parsed with jq
[lisa@linux01 ~/]# cat /etc/group | column -J -s : --table-columns "group,password,gid,members" | jq '[.table[] | {group: .group, members: .members}]'
[
{
"group": "root",
"members": "root,lisa"
},
{
"group": "bin",
"members": null
},
{
"group": "daemon",
"members": null
},
{
"group": "passim",
"members": null
}
]