I've a file which has the following content:
"key1":[
"val11",
"val12",
...
],
"key2":[
"val21",
"val22",
...
],
"key3":[
"val31",
"val32",
...
],
...
I want to extract the values belong to a specific key. Any hints?
Regards,
HZ
I've a file which has the following content:
"key1":[
"val11",
"val12",
...
],
"key2":[
"val21",
"val22",
...
],
"key3":[
"val31",
"val32",
...
],
...
I want to extract the values belong to a specific key. Any hints?
Regards,
HZ
I've a file which has the following content:
I want to extract the values belong to a specific key. Any hints?
On 10.12.2021 15:51, hongy...@gmail.com wrote:
I've a file which has the following content:
"key1":[
"val11",
"val12",
...
],
"key2":[
"val21",
"val22",
...
],
"key3":[
"val31",
"val32",
...
],
...
I want to extract the values belong to a specific key. Any hints?
To get the whole key-block use (for example for "key2")
awk '/"key2":/,/],/'
would produce
"key2":[
"val21",
"val22",
...
],
Adjustments may be necessary if you want something different.
May fail depending on actual data.
Awk is flexible enough to, e.g., omit the first and last line of the
key block, or extract the valXY substrings from the data lines.
Left as homework.
I've a file which has the following content:
"key1":[
"val11",
"val12",
...
],
I want to extract the values belong to a specific key. Any hints?
On 2021-12-10, hongy...@gmail.com wrote:
I've a file which has the following content:
"key1":[
"val11",
"val12",
...
],
I want to extract the values belong to a specific key. Any hints?
This looks like JSON, there's a tool called jq that makes querying
it easy. Try something like `jq -r '.key2[]' < yourfile`.
On Saturday, December 11, 2021 at 6:43:30 AM UTC+8, Tavis Ormandy wrote:
On 2021-12-10, hongy...@gmail.com wrote:
I've a file which has the following content:This looks like JSON, there's a tool called jq that makes querying
"key1":[
"val11",
"val12",
...
],
I want to extract the values belong to a specific key. Any hints?
it easy. Try something like `jq -r '.key2[]' < yourfile`.
In fact, I am processing the "live/packages_choice.json" file coming from the following image:
http://cdimage.deepin.com/releases/20.3/deepin-desktop-community-20.3-amd64.iso
The original file, although named as a json file, lacks matching `{` at the very beginning, which makes the jq tool fails. If I add the missing `{` to the first line of the file, then jq will work, say, for `dde` key:
$ jq -r '.dde[]' < packages_choice.json
On Saturday, December 11, 2021 at 6:43:30 AM UTC+8, Tavis Ormandy wrote:
On 2021-12-10, hongy...@gmail.com wrote:
I've a file which has the following content:This looks like JSON, there's a tool called jq that makes querying
"key1":[
"val11",
"val12",
...
],
I want to extract the values belong to a specific key. Any hints?
it easy. Try something like `jq -r '.key2[]' < yourfile`.
In fact, I am processing the "live/packages_choice.json" file coming from the following image:
http://cdimage.deepin.com/releases/20.3/deepin-desktop-community-20.3-amd64.iso
The original file, although named as a json file, lacks matching `{`
at the very beginning, which makes the jq tool fails. If I add the
missing `{` to the first line of the file, then jq will work, say, for
`dde` key:
$ jq -r '.dde[]' < packages_choice.json[...]
But without adding the missing `{`, the following error will be triggered:
$ jq -r '.dde[]' < /mnt/live/packages_choice.json
jq: error (at <stdin>:2): Cannot index string with string "dde"
parse error: Expected string key before ':' at line 2, column 10
However, both sed and awk can use this poorly formatted json file to[snip]
complete the following task:
$ awk '/"dde":/,/\]/' /mnt/live/packages_choice.json | sed '1d;$d' | awk -F\" '{print $2}'
$ sed -n '/"dde":/,/\]/p' /mnt/live/packages_choice.json | sed '1d;$d' | awk -F\" '{print $2}'[snip]
On 12/10/2021 5:52 PM, hongy...@gmail.com wrote:
On Saturday, December 11, 2021 at 6:43:30 AM UTC+8, Tavis Ormandy wrote:
On 2021-12-10, hongy...@gmail.com wrote:
I've a file which has the following content:This looks like JSON, there's a tool called jq that makes querying
"key1":[
"val11",
"val12",
...
],
I want to extract the values belong to a specific key. Any hints?
it easy. Try something like `jq -r '.key2[]' < yourfile`.
In fact, I am processing the "live/packages_choice.json" file coming from the following image:
http://cdimage.deepin.com/releases/20.3/deepin-desktop-community-20.3-amd64.iso
The original file, although named as a json file, lacks matching `{` at the very beginning, which makes the jq tool fails. If I add the missing `{` to the first line of the file, then jq will work, say, for `dde` key:
$ jq -r '.dde[]' < packages_choice.jsonThen why would you look for anything beyond:
{ echo '{'; cat packages_choice.json; } | jq -r '.dde[]'
or similar? Anything you come up with using sed or awk to parse the JSON
will be far less robust than using `jq` and it doesn't seem necessary if
all you need to do is add a leading `{` to the input and then `jq` works.
On Saturday, December 11, 2021 at 6:43:30 AM UTC+8, Tavis Ormandy wrote:
On 2021-12-10, hongy...@gmail.com wrote:
I've a file which has the following content:
"key1":[
"val11",
"val12",
...
],
I want to extract the values belong to a specific key. Any hints?
This looks like JSON, there's a tool called jq that makes queryingIn fact, I am processing the "live/packages_choice.json" file coming from the following image:
it easy. Try something like `jq -r '.key2[]' < yourfile`.
http://cdimage.deepin.com/releases/20.3/deepin-desktop-community-20.3-amd64.iso
The original file, although named as a json file, lacks matching `{` at the very beginning, which makes the jq tool fails. If I add the missing `{` to the first line of the file, then jq will work, say, for `dde` key:
$ jq -r '.dde[]' < packages_choice.json
deepin-desktop-server
deepin-default-settings
dde-desktop
dde-dock
dde-launcher
dde-control-center
startdde
dde-session-ui
deepin-artwork
dde-file-manager
dde-qt5integration
plymouth-theme-deepin-logo
deepin-wallpapers
fonts-noto
dde-introduction
dde-kwin
deepin-screensaver
dde
dde-calendar
network-manager-integration-plugins
deepin-terminal
But without adding the missing `{`, the following error will be triggered:
$ jq -r '.dde[]' < /mnt/live/packages_choice.json
jq: error (at <stdin>:2): Cannot index string with string "dde"
parse error: Expected string key before ':' at line 2, column 10
However, both sed and awk can use this poorly formatted json file to complete the following task:
$ awk '/"dde":/,/\]/' /mnt/live/packages_choice.json | sed '1d;$d' | awk -F\" '{print $2}'
deepin-desktop-server
deepin-default-settings
dde-desktop
dde-dock
dde-launcher
dde-control-center
startdde
dde-session-ui
deepin-artwork
dde-file-manager
dde-qt5integration
plymouth-theme-deepin-logo
deepin-wallpapers
fonts-noto
dde-introduction
dde-kwin
deepin-screensaver
dde
dde-calendar
network-manager-integration-plugins
deepin-terminal
$ sed -n '/"dde":/,/\]/p' /mnt/live/packages_choice.json | sed '1d;$d' | awk -F\" '{print $2}'
deepin-desktop-server
deepin-default-settings
dde-desktop
dde-dock
dde-launcher
dde-control-center
startdde
dde-session-ui
deepin-artwork
dde-file-manager
dde-qt5integration
plymouth-theme-deepin-logo
deepin-wallpapers
fonts-noto
dde-introduction
dde-kwin
deepin-screensaver
dde
dde-calendar
network-manager-integration-plugins
deepin-terminal
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 293 |
Nodes: | 16 (2 / 14) |
Uptime: | 231:05:19 |
Calls: | 6,624 |
Files: | 12,171 |
Messages: | 5,319,316 |