Sed, Regular expression to get substring [on hold]
Clash Royale CLAN TAG#URR8PPP
up vote
-5
down vote
favorite
I am running this command to extract some entries from docker
output:
out=$(sudo docker inspect fabric-lifecycle);echo $out | sed -n 's/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "PublishAllPorts".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;7;8/p'
I would like to include the "Memory" entry in the output too, so that the complete output is
e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e;running;95168;0;fabric-lifecycle;false;[ "lifecycle" ];caspian/fabric:3.3.0.0-3594.2b8e92d;1073741824
Here is the full value of $out
:
[
"Id": "e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e",
"Created": "2018-10-01T05:34:37.972705474Z",
"Path": "./boot.sh",
"Args": [
"lifecycle"
],
"State":
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 95168,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-10-01T05:34:51.739952918Z",
"FinishedAt": "2018-10-01T05:34:46.361850627Z"
,
"Image": "sha256:328eab472dbd625983c23f8760bc3d0b595dd161ee0d12e1d6d53c8b70228fd1",
"ResolvConfPath": "/var/lib/docker/containers/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e/hostname",
"HostsPath": "/var/lib/docker/containers/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e/hosts",
"LogPath": "/var/lib/docker/containers/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e-json.log",
"Name": "/fabric-lifecycle",
"RestartCount": 0,
"Driver": "devicemapper",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig":
"Binds": [
"/opt/emc/caspian/fabric/agent/services/fabric/data:/data:rw",
"/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/log:/var/log:rw",
"/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/host/data:/host/data:ro",
"/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/host/files:/host/files:ro"
],
"ContainerIDFile": "",
"LogConfig":
"Type": "json-file",
"Config":
,
"NetworkMode": "host",
"PortBindings": null,
"RestartPolicy":
"Name": "",
"MaximumRetryCount": 0
,
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": null,
"DnsOptions": null,
"DnsSearch": null,
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 1073741824,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": null,
"DeviceCgroupRules": null,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": -1,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
,
"GraphDriver":
"Data":
"DeviceId": "119",
"DeviceName": "docker-254:0-354418880-9759c901ef5109a23a913840bd789fa6934ac18a4688ba60cabbad13b15078b8",
"DeviceSize": "10737418240"
,
"Name": "devicemapper"
,
"Mounts": [
"Type": "bind",
"Source": "/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/host/data",
"Destination": "/host/data",
"Mode": "ro",
"RW": false,
"Propagation": "rprivate"
,
"Type": "bind",
"Source": "/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/host/files",
"Destination": "/host/files",
"Mode": "ro",
"RW": false,
"Propagation": "rprivate"
,
"Type": "bind",
"Source": "/opt/emc/caspian/fabric/agent/services/fabric/data",
"Destination": "/data",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
,
"Type": "bind",
"Source": "/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/log",
"Destination": "/var/log",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
],
"Config":
"Hostname": "provo-burlywood",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"ZYPPER=zypper --no-gpg-checks --non-interactive",
"JAVA_HOME=/usr/lib64/jvm/jre-1.8.0-oracle/",
"APPLICATION=fabric",
"ROLE=lifecycle",
"VERSION=3.3.0.0-3594.2b8e92d",
"DOCKER_FIX= "
],
"Cmd": [
"lifecycle"
],
"Image": "caspian/fabric:3.3.0.0-3594.2b8e92d",
"Volumes": null,
"WorkingDir": "/opt/emc/caspian/fabric/lifecycle",
"Entrypoint": [
"./boot.sh"
],
"OnBuild": null,
"Labels":
,
"NetworkSettings":
"Bridge": "",
"SandboxID": "683a8af8564b3083ac766b7065d5c84315ae8d2d0898c5099baccf2e137e7ca6",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": ,
"SandboxKey": "/var/run/docker/netns/default",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks":
"host":
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "d8f7b03c732d5c57d170d76211c33710a17c8f670a5d8c9ebb91a0bdc2f8bb46",
"EndpointID": "ef788fd7eaa43369a184287c88ea19a7f5f369c808d3af40b9df41579a1251a6",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
]
Output should be:
sed regular-expression
New contributor
put on hold as unclear what you're asking by jimmij, Goro, dr01, Kiwy, Jeff Schaller Oct 4 at 11:51
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |Â
up vote
-5
down vote
favorite
I am running this command to extract some entries from docker
output:
out=$(sudo docker inspect fabric-lifecycle);echo $out | sed -n 's/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "PublishAllPorts".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;7;8/p'
I would like to include the "Memory" entry in the output too, so that the complete output is
e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e;running;95168;0;fabric-lifecycle;false;[ "lifecycle" ];caspian/fabric:3.3.0.0-3594.2b8e92d;1073741824
Here is the full value of $out
:
[
"Id": "e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e",
"Created": "2018-10-01T05:34:37.972705474Z",
"Path": "./boot.sh",
"Args": [
"lifecycle"
],
"State":
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 95168,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-10-01T05:34:51.739952918Z",
"FinishedAt": "2018-10-01T05:34:46.361850627Z"
,
"Image": "sha256:328eab472dbd625983c23f8760bc3d0b595dd161ee0d12e1d6d53c8b70228fd1",
"ResolvConfPath": "/var/lib/docker/containers/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e/hostname",
"HostsPath": "/var/lib/docker/containers/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e/hosts",
"LogPath": "/var/lib/docker/containers/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e-json.log",
"Name": "/fabric-lifecycle",
"RestartCount": 0,
"Driver": "devicemapper",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig":
"Binds": [
"/opt/emc/caspian/fabric/agent/services/fabric/data:/data:rw",
"/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/log:/var/log:rw",
"/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/host/data:/host/data:ro",
"/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/host/files:/host/files:ro"
],
"ContainerIDFile": "",
"LogConfig":
"Type": "json-file",
"Config":
,
"NetworkMode": "host",
"PortBindings": null,
"RestartPolicy":
"Name": "",
"MaximumRetryCount": 0
,
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": null,
"DnsOptions": null,
"DnsSearch": null,
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 1073741824,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": null,
"DeviceCgroupRules": null,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": -1,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
,
"GraphDriver":
"Data":
"DeviceId": "119",
"DeviceName": "docker-254:0-354418880-9759c901ef5109a23a913840bd789fa6934ac18a4688ba60cabbad13b15078b8",
"DeviceSize": "10737418240"
,
"Name": "devicemapper"
,
"Mounts": [
"Type": "bind",
"Source": "/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/host/data",
"Destination": "/host/data",
"Mode": "ro",
"RW": false,
"Propagation": "rprivate"
,
"Type": "bind",
"Source": "/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/host/files",
"Destination": "/host/files",
"Mode": "ro",
"RW": false,
"Propagation": "rprivate"
,
"Type": "bind",
"Source": "/opt/emc/caspian/fabric/agent/services/fabric/data",
"Destination": "/data",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
,
"Type": "bind",
"Source": "/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/log",
"Destination": "/var/log",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
],
"Config":
"Hostname": "provo-burlywood",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"ZYPPER=zypper --no-gpg-checks --non-interactive",
"JAVA_HOME=/usr/lib64/jvm/jre-1.8.0-oracle/",
"APPLICATION=fabric",
"ROLE=lifecycle",
"VERSION=3.3.0.0-3594.2b8e92d",
"DOCKER_FIX= "
],
"Cmd": [
"lifecycle"
],
"Image": "caspian/fabric:3.3.0.0-3594.2b8e92d",
"Volumes": null,
"WorkingDir": "/opt/emc/caspian/fabric/lifecycle",
"Entrypoint": [
"./boot.sh"
],
"OnBuild": null,
"Labels":
,
"NetworkSettings":
"Bridge": "",
"SandboxID": "683a8af8564b3083ac766b7065d5c84315ae8d2d0898c5099baccf2e137e7ca6",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": ,
"SandboxKey": "/var/run/docker/netns/default",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks":
"host":
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "d8f7b03c732d5c57d170d76211c33710a17c8f670a5d8c9ebb91a0bdc2f8bb46",
"EndpointID": "ef788fd7eaa43369a184287c88ea19a7f5f369c808d3af40b9df41579a1251a6",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
]
Output should be:
sed regular-expression
New contributor
put on hold as unclear what you're asking by jimmij, Goro, dr01, Kiwy, Jeff Schaller Oct 4 at 11:51
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Hello and welcome to StackExchange. It is very unclear what you're asking. Please edit the question to provide more details, for example the desired output.
â Panki
Oct 4 at 8:41
1
Also to deal with json it's usually easier to use json parser tool.
â Kiwy
Oct 4 at 11:30
add a comment |Â
up vote
-5
down vote
favorite
up vote
-5
down vote
favorite
I am running this command to extract some entries from docker
output:
out=$(sudo docker inspect fabric-lifecycle);echo $out | sed -n 's/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "PublishAllPorts".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;7;8/p'
I would like to include the "Memory" entry in the output too, so that the complete output is
e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e;running;95168;0;fabric-lifecycle;false;[ "lifecycle" ];caspian/fabric:3.3.0.0-3594.2b8e92d;1073741824
Here is the full value of $out
:
[
"Id": "e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e",
"Created": "2018-10-01T05:34:37.972705474Z",
"Path": "./boot.sh",
"Args": [
"lifecycle"
],
"State":
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 95168,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-10-01T05:34:51.739952918Z",
"FinishedAt": "2018-10-01T05:34:46.361850627Z"
,
"Image": "sha256:328eab472dbd625983c23f8760bc3d0b595dd161ee0d12e1d6d53c8b70228fd1",
"ResolvConfPath": "/var/lib/docker/containers/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e/hostname",
"HostsPath": "/var/lib/docker/containers/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e/hosts",
"LogPath": "/var/lib/docker/containers/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e-json.log",
"Name": "/fabric-lifecycle",
"RestartCount": 0,
"Driver": "devicemapper",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig":
"Binds": [
"/opt/emc/caspian/fabric/agent/services/fabric/data:/data:rw",
"/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/log:/var/log:rw",
"/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/host/data:/host/data:ro",
"/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/host/files:/host/files:ro"
],
"ContainerIDFile": "",
"LogConfig":
"Type": "json-file",
"Config":
,
"NetworkMode": "host",
"PortBindings": null,
"RestartPolicy":
"Name": "",
"MaximumRetryCount": 0
,
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": null,
"DnsOptions": null,
"DnsSearch": null,
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 1073741824,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": null,
"DeviceCgroupRules": null,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": -1,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
,
"GraphDriver":
"Data":
"DeviceId": "119",
"DeviceName": "docker-254:0-354418880-9759c901ef5109a23a913840bd789fa6934ac18a4688ba60cabbad13b15078b8",
"DeviceSize": "10737418240"
,
"Name": "devicemapper"
,
"Mounts": [
"Type": "bind",
"Source": "/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/host/data",
"Destination": "/host/data",
"Mode": "ro",
"RW": false,
"Propagation": "rprivate"
,
"Type": "bind",
"Source": "/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/host/files",
"Destination": "/host/files",
"Mode": "ro",
"RW": false,
"Propagation": "rprivate"
,
"Type": "bind",
"Source": "/opt/emc/caspian/fabric/agent/services/fabric/data",
"Destination": "/data",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
,
"Type": "bind",
"Source": "/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/log",
"Destination": "/var/log",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
],
"Config":
"Hostname": "provo-burlywood",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"ZYPPER=zypper --no-gpg-checks --non-interactive",
"JAVA_HOME=/usr/lib64/jvm/jre-1.8.0-oracle/",
"APPLICATION=fabric",
"ROLE=lifecycle",
"VERSION=3.3.0.0-3594.2b8e92d",
"DOCKER_FIX= "
],
"Cmd": [
"lifecycle"
],
"Image": "caspian/fabric:3.3.0.0-3594.2b8e92d",
"Volumes": null,
"WorkingDir": "/opt/emc/caspian/fabric/lifecycle",
"Entrypoint": [
"./boot.sh"
],
"OnBuild": null,
"Labels":
,
"NetworkSettings":
"Bridge": "",
"SandboxID": "683a8af8564b3083ac766b7065d5c84315ae8d2d0898c5099baccf2e137e7ca6",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": ,
"SandboxKey": "/var/run/docker/netns/default",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks":
"host":
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "d8f7b03c732d5c57d170d76211c33710a17c8f670a5d8c9ebb91a0bdc2f8bb46",
"EndpointID": "ef788fd7eaa43369a184287c88ea19a7f5f369c808d3af40b9df41579a1251a6",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
]
Output should be:
sed regular-expression
New contributor
I am running this command to extract some entries from docker
output:
out=$(sudo docker inspect fabric-lifecycle);echo $out | sed -n 's/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "PublishAllPorts".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;7;8/p'
I would like to include the "Memory" entry in the output too, so that the complete output is
e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e;running;95168;0;fabric-lifecycle;false;[ "lifecycle" ];caspian/fabric:3.3.0.0-3594.2b8e92d;1073741824
Here is the full value of $out
:
[
"Id": "e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e",
"Created": "2018-10-01T05:34:37.972705474Z",
"Path": "./boot.sh",
"Args": [
"lifecycle"
],
"State":
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 95168,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-10-01T05:34:51.739952918Z",
"FinishedAt": "2018-10-01T05:34:46.361850627Z"
,
"Image": "sha256:328eab472dbd625983c23f8760bc3d0b595dd161ee0d12e1d6d53c8b70228fd1",
"ResolvConfPath": "/var/lib/docker/containers/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e/hostname",
"HostsPath": "/var/lib/docker/containers/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e/hosts",
"LogPath": "/var/lib/docker/containers/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e/e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e-json.log",
"Name": "/fabric-lifecycle",
"RestartCount": 0,
"Driver": "devicemapper",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig":
"Binds": [
"/opt/emc/caspian/fabric/agent/services/fabric/data:/data:rw",
"/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/log:/var/log:rw",
"/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/host/data:/host/data:ro",
"/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/host/files:/host/files:ro"
],
"ContainerIDFile": "",
"LogConfig":
"Type": "json-file",
"Config":
,
"NetworkMode": "host",
"PortBindings": null,
"RestartPolicy":
"Name": "",
"MaximumRetryCount": 0
,
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": null,
"DnsOptions": null,
"DnsSearch": null,
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 1073741824,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": null,
"DeviceCgroupRules": null,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": -1,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
,
"GraphDriver":
"Data":
"DeviceId": "119",
"DeviceName": "docker-254:0-354418880-9759c901ef5109a23a913840bd789fa6934ac18a4688ba60cabbad13b15078b8",
"DeviceSize": "10737418240"
,
"Name": "devicemapper"
,
"Mounts": [
"Type": "bind",
"Source": "/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/host/data",
"Destination": "/host/data",
"Mode": "ro",
"RW": false,
"Propagation": "rprivate"
,
"Type": "bind",
"Source": "/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/host/files",
"Destination": "/host/files",
"Mode": "ro",
"RW": false,
"Propagation": "rprivate"
,
"Type": "bind",
"Source": "/opt/emc/caspian/fabric/agent/services/fabric/data",
"Destination": "/data",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
,
"Type": "bind",
"Source": "/opt/emc/caspian/fabric/agent/services/fabric/lifecycle/log",
"Destination": "/var/log",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
],
"Config":
"Hostname": "provo-burlywood",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"ZYPPER=zypper --no-gpg-checks --non-interactive",
"JAVA_HOME=/usr/lib64/jvm/jre-1.8.0-oracle/",
"APPLICATION=fabric",
"ROLE=lifecycle",
"VERSION=3.3.0.0-3594.2b8e92d",
"DOCKER_FIX= "
],
"Cmd": [
"lifecycle"
],
"Image": "caspian/fabric:3.3.0.0-3594.2b8e92d",
"Volumes": null,
"WorkingDir": "/opt/emc/caspian/fabric/lifecycle",
"Entrypoint": [
"./boot.sh"
],
"OnBuild": null,
"Labels":
,
"NetworkSettings":
"Bridge": "",
"SandboxID": "683a8af8564b3083ac766b7065d5c84315ae8d2d0898c5099baccf2e137e7ca6",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": ,
"SandboxKey": "/var/run/docker/netns/default",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks":
"host":
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "d8f7b03c732d5c57d170d76211c33710a17c8f670a5d8c9ebb91a0bdc2f8bb46",
"EndpointID": "ef788fd7eaa43369a184287c88ea19a7f5f369c808d3af40b9df41579a1251a6",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
]
Output should be:
sed regular-expression
sed regular-expression
New contributor
New contributor
edited Oct 4 at 9:43
JigglyNaga
3,062626
3,062626
New contributor
asked Oct 4 at 8:38
Kalapriya Pasupuleti
11
11
New contributor
New contributor
put on hold as unclear what you're asking by jimmij, Goro, dr01, Kiwy, Jeff Schaller Oct 4 at 11:51
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as unclear what you're asking by jimmij, Goro, dr01, Kiwy, Jeff Schaller Oct 4 at 11:51
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Hello and welcome to StackExchange. It is very unclear what you're asking. Please edit the question to provide more details, for example the desired output.
â Panki
Oct 4 at 8:41
1
Also to deal with json it's usually easier to use json parser tool.
â Kiwy
Oct 4 at 11:30
add a comment |Â
Hello and welcome to StackExchange. It is very unclear what you're asking. Please edit the question to provide more details, for example the desired output.
â Panki
Oct 4 at 8:41
1
Also to deal with json it's usually easier to use json parser tool.
â Kiwy
Oct 4 at 11:30
Hello and welcome to StackExchange. It is very unclear what you're asking. Please edit the question to provide more details, for example the desired output.
â Panki
Oct 4 at 8:41
Hello and welcome to StackExchange. It is very unclear what you're asking. Please edit the question to provide more details, for example the desired output.
â Panki
Oct 4 at 8:41
1
1
Also to deal with json it's usually easier to use json parser tool.
â Kiwy
Oct 4 at 11:30
Also to deal with json it's usually easier to use json parser tool.
â Kiwy
Oct 4 at 11:30
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
0
down vote
Don't use sed
to parse large JSON structures. That command is already long and unwieldly, and relies on exact input order -- it will break if some entries swap places within the JSON input, or if another entry appears/disappears after the values you are interested in.
Instead, you could use a dedicated JSON parser, such as jq. Your original command can be replaced with
echo "$out" | jq -c '.|.Id,(.State|.Status,.Pid,.ExitCode),.Name,.HostConfig.Privileged,.Config.Cmd,.Config.Image'
Note that you will have to look at the complete hierarchy of the JSON structure: "Privileged" is part of "HostConfig", "Pid" is part of "State", etc. The command can be split into multiple lines (and called with jq -f file.jq
) as follows:
.| # run the following on all entries in the list
.Id,
(.State| #extract following members from .State:
.Status, .Pid, .ExitCode
),
.Name,
.HostConfig.Privileged,
.Config.Cmd,
.Config.Image
This gives the following output:
"e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e"
"running"
95168
0
"/fabric-lifecycle"
false
["lifecycle"]
"caspian/fabric:3.3.0.0-3594.2b8e92d"
(If you want to join that into a single comma-separated line, append | tr 'n' ,
to the pipeline.)
Then you can include Memory
by appending .HostConfig.Memory
to the list:
jq -c '.|.Id,(.State|.Status,.Pid,.ExitCode),.Name,.HostConfig.Privileged,.Config.Cmd,.Config.Image,.HostConfig.Memory'
Thanks for the reply.But,I wanted to add only memory to already existing sed command ..
â Kalapriya Pasupuleti
Oct 4 at 11:01
I tried this but dint help :out=$(sudo docker inspect fabric-lifecycle);echo $out | sed -n 's/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;7;8;9/p'
â Kalapriya Pasupuleti
Oct 4 at 11:01
I dont have jq ,I have to use only sed .that is one limitation ..Hence need solution with sed ..
â Kalapriya Pasupuleti
Oct 4 at 11:14
Please edit your question to show what you've tried, and how it went wrong (eg. any error messages). You can scroll down to see a preview, to check that it is formatted correctly.
â JigglyNaga
Oct 4 at 11:16
out=$(sudo docker inspect fabric-lifecycle);echo $out | sed -n 's/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;7;8;9/p' admin@jfhsfh:~> Added Memory but still no output
â Kalapriya Pasupuleti
Oct 4 at 11:21
 |Â
show 2 more comments
up vote
0
down vote
I can't stress enough that sed
is an unsuitable tool for parsing arbitrary nested structures like JSON. But if you are forced down this path...
Simple match based on next entry name
Let's break down what your first sed
command is doing. It relies on matching the entry names of (hopefully) consecutive lines, and capturing what's between them:
.*"Privileged": (.*), "PublishAllPorts".*
matches these lines:
"Privileged": false,
"PublishAllPorts": false,
capturing the false
at the end of the first line.
You then changed that part into:
.*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*
That will no longer match, because the entry "Memory"
does not appear at that point in the file. If you want to match "Memory"
in the same manner as before, you will have to see what comes next. From your docker
output:
"Memory": 1073741824,
"NanoCpus": 0,
so you'll have to add this in the right place in your pattern:
"Memory": (.*), "NanoCpus"
All together, your sed
command will have to look like this:
sed -n `s/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "PublishAllPorts".*"Memory": (.*), "NanoCpus".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;8;9;7/p`
Note that 7
has to go at the end of the replacement, as you wanted "Memory" to appear last.
Stricter matching based on content
Rather than relying on the next entry name, you could capture only the contents that you expect for the entry, using stricter patterns than (.*)
. This still requires some context for the "Image" entry. I'm using the direct output from docker
, rather than saving into a variable and echo
, so the linebreaks are maintained. This means I also need some extra sed
trickery to join the "Cmd" back into a single line, and warrants putting the whole sed
command into a script.
sudo docker inspect fabric-lifecycle | sed -f extract.sed
Contents of extract.sed
:
#no echo
s/^s*"Id": "([^"]*)",s*$/1/p # anything but the closing "
s/^s*"Status": "([^"]*)",s*$/1/p
s/^s*"Pid": ([0-9]*),s*$/1/p # decimal number
s/^s*"ExitCode": ([0-9]*),s*$/1/p
s/^s*"Name": "/([^"]*)",s*$/1/p # match from "/ to closing "
s/^s*"Privileged": ([^,]*),s*$/1/p # match up to comma (unquoted boolean)
/^s*"Cmd": [/
:more
s/s+/ /g # collapse whitespace
/],s*$/bb # if closing ] found, jump to "final"
N # append next line
ba # loop to "more"
:final
s/^.*"Cmd": ([.*]),s*$/1/p # capture everything inside [...]
/^s*"Config": $/,/^s*$/ # only match next line inside "Config"
s/^s*"Image": "([^"]*)",s*$/1/p
s/^s*"Memory": ([0-9]*),s*$/1/p
This produces the output:
e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e
running
95168
0
fabric-lifecycle
false
1073741824
[ "lifecycle" ]
caspian/fabric:3.3.0.0-3594.2b8e92d
Wow..Thank you so much JigglyNaga ...Thanks for your patience ..
â Kalapriya Pasupuleti
Oct 4 at 12:34
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Don't use sed
to parse large JSON structures. That command is already long and unwieldly, and relies on exact input order -- it will break if some entries swap places within the JSON input, or if another entry appears/disappears after the values you are interested in.
Instead, you could use a dedicated JSON parser, such as jq. Your original command can be replaced with
echo "$out" | jq -c '.|.Id,(.State|.Status,.Pid,.ExitCode),.Name,.HostConfig.Privileged,.Config.Cmd,.Config.Image'
Note that you will have to look at the complete hierarchy of the JSON structure: "Privileged" is part of "HostConfig", "Pid" is part of "State", etc. The command can be split into multiple lines (and called with jq -f file.jq
) as follows:
.| # run the following on all entries in the list
.Id,
(.State| #extract following members from .State:
.Status, .Pid, .ExitCode
),
.Name,
.HostConfig.Privileged,
.Config.Cmd,
.Config.Image
This gives the following output:
"e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e"
"running"
95168
0
"/fabric-lifecycle"
false
["lifecycle"]
"caspian/fabric:3.3.0.0-3594.2b8e92d"
(If you want to join that into a single comma-separated line, append | tr 'n' ,
to the pipeline.)
Then you can include Memory
by appending .HostConfig.Memory
to the list:
jq -c '.|.Id,(.State|.Status,.Pid,.ExitCode),.Name,.HostConfig.Privileged,.Config.Cmd,.Config.Image,.HostConfig.Memory'
Thanks for the reply.But,I wanted to add only memory to already existing sed command ..
â Kalapriya Pasupuleti
Oct 4 at 11:01
I tried this but dint help :out=$(sudo docker inspect fabric-lifecycle);echo $out | sed -n 's/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;7;8;9/p'
â Kalapriya Pasupuleti
Oct 4 at 11:01
I dont have jq ,I have to use only sed .that is one limitation ..Hence need solution with sed ..
â Kalapriya Pasupuleti
Oct 4 at 11:14
Please edit your question to show what you've tried, and how it went wrong (eg. any error messages). You can scroll down to see a preview, to check that it is formatted correctly.
â JigglyNaga
Oct 4 at 11:16
out=$(sudo docker inspect fabric-lifecycle);echo $out | sed -n 's/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;7;8;9/p' admin@jfhsfh:~> Added Memory but still no output
â Kalapriya Pasupuleti
Oct 4 at 11:21
 |Â
show 2 more comments
up vote
0
down vote
Don't use sed
to parse large JSON structures. That command is already long and unwieldly, and relies on exact input order -- it will break if some entries swap places within the JSON input, or if another entry appears/disappears after the values you are interested in.
Instead, you could use a dedicated JSON parser, such as jq. Your original command can be replaced with
echo "$out" | jq -c '.|.Id,(.State|.Status,.Pid,.ExitCode),.Name,.HostConfig.Privileged,.Config.Cmd,.Config.Image'
Note that you will have to look at the complete hierarchy of the JSON structure: "Privileged" is part of "HostConfig", "Pid" is part of "State", etc. The command can be split into multiple lines (and called with jq -f file.jq
) as follows:
.| # run the following on all entries in the list
.Id,
(.State| #extract following members from .State:
.Status, .Pid, .ExitCode
),
.Name,
.HostConfig.Privileged,
.Config.Cmd,
.Config.Image
This gives the following output:
"e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e"
"running"
95168
0
"/fabric-lifecycle"
false
["lifecycle"]
"caspian/fabric:3.3.0.0-3594.2b8e92d"
(If you want to join that into a single comma-separated line, append | tr 'n' ,
to the pipeline.)
Then you can include Memory
by appending .HostConfig.Memory
to the list:
jq -c '.|.Id,(.State|.Status,.Pid,.ExitCode),.Name,.HostConfig.Privileged,.Config.Cmd,.Config.Image,.HostConfig.Memory'
Thanks for the reply.But,I wanted to add only memory to already existing sed command ..
â Kalapriya Pasupuleti
Oct 4 at 11:01
I tried this but dint help :out=$(sudo docker inspect fabric-lifecycle);echo $out | sed -n 's/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;7;8;9/p'
â Kalapriya Pasupuleti
Oct 4 at 11:01
I dont have jq ,I have to use only sed .that is one limitation ..Hence need solution with sed ..
â Kalapriya Pasupuleti
Oct 4 at 11:14
Please edit your question to show what you've tried, and how it went wrong (eg. any error messages). You can scroll down to see a preview, to check that it is formatted correctly.
â JigglyNaga
Oct 4 at 11:16
out=$(sudo docker inspect fabric-lifecycle);echo $out | sed -n 's/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;7;8;9/p' admin@jfhsfh:~> Added Memory but still no output
â Kalapriya Pasupuleti
Oct 4 at 11:21
 |Â
show 2 more comments
up vote
0
down vote
up vote
0
down vote
Don't use sed
to parse large JSON structures. That command is already long and unwieldly, and relies on exact input order -- it will break if some entries swap places within the JSON input, or if another entry appears/disappears after the values you are interested in.
Instead, you could use a dedicated JSON parser, such as jq. Your original command can be replaced with
echo "$out" | jq -c '.|.Id,(.State|.Status,.Pid,.ExitCode),.Name,.HostConfig.Privileged,.Config.Cmd,.Config.Image'
Note that you will have to look at the complete hierarchy of the JSON structure: "Privileged" is part of "HostConfig", "Pid" is part of "State", etc. The command can be split into multiple lines (and called with jq -f file.jq
) as follows:
.| # run the following on all entries in the list
.Id,
(.State| #extract following members from .State:
.Status, .Pid, .ExitCode
),
.Name,
.HostConfig.Privileged,
.Config.Cmd,
.Config.Image
This gives the following output:
"e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e"
"running"
95168
0
"/fabric-lifecycle"
false
["lifecycle"]
"caspian/fabric:3.3.0.0-3594.2b8e92d"
(If you want to join that into a single comma-separated line, append | tr 'n' ,
to the pipeline.)
Then you can include Memory
by appending .HostConfig.Memory
to the list:
jq -c '.|.Id,(.State|.Status,.Pid,.ExitCode),.Name,.HostConfig.Privileged,.Config.Cmd,.Config.Image,.HostConfig.Memory'
Don't use sed
to parse large JSON structures. That command is already long and unwieldly, and relies on exact input order -- it will break if some entries swap places within the JSON input, or if another entry appears/disappears after the values you are interested in.
Instead, you could use a dedicated JSON parser, such as jq. Your original command can be replaced with
echo "$out" | jq -c '.|.Id,(.State|.Status,.Pid,.ExitCode),.Name,.HostConfig.Privileged,.Config.Cmd,.Config.Image'
Note that you will have to look at the complete hierarchy of the JSON structure: "Privileged" is part of "HostConfig", "Pid" is part of "State", etc. The command can be split into multiple lines (and called with jq -f file.jq
) as follows:
.| # run the following on all entries in the list
.Id,
(.State| #extract following members from .State:
.Status, .Pid, .ExitCode
),
.Name,
.HostConfig.Privileged,
.Config.Cmd,
.Config.Image
This gives the following output:
"e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e"
"running"
95168
0
"/fabric-lifecycle"
false
["lifecycle"]
"caspian/fabric:3.3.0.0-3594.2b8e92d"
(If you want to join that into a single comma-separated line, append | tr 'n' ,
to the pipeline.)
Then you can include Memory
by appending .HostConfig.Memory
to the list:
jq -c '.|.Id,(.State|.Status,.Pid,.ExitCode),.Name,.HostConfig.Privileged,.Config.Cmd,.Config.Image,.HostConfig.Memory'
answered Oct 4 at 10:39
JigglyNaga
3,062626
3,062626
Thanks for the reply.But,I wanted to add only memory to already existing sed command ..
â Kalapriya Pasupuleti
Oct 4 at 11:01
I tried this but dint help :out=$(sudo docker inspect fabric-lifecycle);echo $out | sed -n 's/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;7;8;9/p'
â Kalapriya Pasupuleti
Oct 4 at 11:01
I dont have jq ,I have to use only sed .that is one limitation ..Hence need solution with sed ..
â Kalapriya Pasupuleti
Oct 4 at 11:14
Please edit your question to show what you've tried, and how it went wrong (eg. any error messages). You can scroll down to see a preview, to check that it is formatted correctly.
â JigglyNaga
Oct 4 at 11:16
out=$(sudo docker inspect fabric-lifecycle);echo $out | sed -n 's/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;7;8;9/p' admin@jfhsfh:~> Added Memory but still no output
â Kalapriya Pasupuleti
Oct 4 at 11:21
 |Â
show 2 more comments
Thanks for the reply.But,I wanted to add only memory to already existing sed command ..
â Kalapriya Pasupuleti
Oct 4 at 11:01
I tried this but dint help :out=$(sudo docker inspect fabric-lifecycle);echo $out | sed -n 's/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;7;8;9/p'
â Kalapriya Pasupuleti
Oct 4 at 11:01
I dont have jq ,I have to use only sed .that is one limitation ..Hence need solution with sed ..
â Kalapriya Pasupuleti
Oct 4 at 11:14
Please edit your question to show what you've tried, and how it went wrong (eg. any error messages). You can scroll down to see a preview, to check that it is formatted correctly.
â JigglyNaga
Oct 4 at 11:16
out=$(sudo docker inspect fabric-lifecycle);echo $out | sed -n 's/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;7;8;9/p' admin@jfhsfh:~> Added Memory but still no output
â Kalapriya Pasupuleti
Oct 4 at 11:21
Thanks for the reply.But,I wanted to add only memory to already existing sed command ..
â Kalapriya Pasupuleti
Oct 4 at 11:01
Thanks for the reply.But,I wanted to add only memory to already existing sed command ..
â Kalapriya Pasupuleti
Oct 4 at 11:01
I tried this but dint help :out=$(sudo docker inspect fabric-lifecycle);echo $out | sed -n 's/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;7;8;9/p'
â Kalapriya Pasupuleti
Oct 4 at 11:01
I tried this but dint help :out=$(sudo docker inspect fabric-lifecycle);echo $out | sed -n 's/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;7;8;9/p'
â Kalapriya Pasupuleti
Oct 4 at 11:01
I dont have jq ,I have to use only sed .that is one limitation ..Hence need solution with sed ..
â Kalapriya Pasupuleti
Oct 4 at 11:14
I dont have jq ,I have to use only sed .that is one limitation ..Hence need solution with sed ..
â Kalapriya Pasupuleti
Oct 4 at 11:14
Please edit your question to show what you've tried, and how it went wrong (eg. any error messages). You can scroll down to see a preview, to check that it is formatted correctly.
â JigglyNaga
Oct 4 at 11:16
Please edit your question to show what you've tried, and how it went wrong (eg. any error messages). You can scroll down to see a preview, to check that it is formatted correctly.
â JigglyNaga
Oct 4 at 11:16
out=$(sudo docker inspect fabric-lifecycle);echo $out | sed -n 's/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;7;8;9/p' admin@jfhsfh:~> Added Memory but still no output
â Kalapriya Pasupuleti
Oct 4 at 11:21
out=$(sudo docker inspect fabric-lifecycle);echo $out | sed -n 's/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;7;8;9/p' admin@jfhsfh:~> Added Memory but still no output
â Kalapriya Pasupuleti
Oct 4 at 11:21
 |Â
show 2 more comments
up vote
0
down vote
I can't stress enough that sed
is an unsuitable tool for parsing arbitrary nested structures like JSON. But if you are forced down this path...
Simple match based on next entry name
Let's break down what your first sed
command is doing. It relies on matching the entry names of (hopefully) consecutive lines, and capturing what's between them:
.*"Privileged": (.*), "PublishAllPorts".*
matches these lines:
"Privileged": false,
"PublishAllPorts": false,
capturing the false
at the end of the first line.
You then changed that part into:
.*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*
That will no longer match, because the entry "Memory"
does not appear at that point in the file. If you want to match "Memory"
in the same manner as before, you will have to see what comes next. From your docker
output:
"Memory": 1073741824,
"NanoCpus": 0,
so you'll have to add this in the right place in your pattern:
"Memory": (.*), "NanoCpus"
All together, your sed
command will have to look like this:
sed -n `s/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "PublishAllPorts".*"Memory": (.*), "NanoCpus".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;8;9;7/p`
Note that 7
has to go at the end of the replacement, as you wanted "Memory" to appear last.
Stricter matching based on content
Rather than relying on the next entry name, you could capture only the contents that you expect for the entry, using stricter patterns than (.*)
. This still requires some context for the "Image" entry. I'm using the direct output from docker
, rather than saving into a variable and echo
, so the linebreaks are maintained. This means I also need some extra sed
trickery to join the "Cmd" back into a single line, and warrants putting the whole sed
command into a script.
sudo docker inspect fabric-lifecycle | sed -f extract.sed
Contents of extract.sed
:
#no echo
s/^s*"Id": "([^"]*)",s*$/1/p # anything but the closing "
s/^s*"Status": "([^"]*)",s*$/1/p
s/^s*"Pid": ([0-9]*),s*$/1/p # decimal number
s/^s*"ExitCode": ([0-9]*),s*$/1/p
s/^s*"Name": "/([^"]*)",s*$/1/p # match from "/ to closing "
s/^s*"Privileged": ([^,]*),s*$/1/p # match up to comma (unquoted boolean)
/^s*"Cmd": [/
:more
s/s+/ /g # collapse whitespace
/],s*$/bb # if closing ] found, jump to "final"
N # append next line
ba # loop to "more"
:final
s/^.*"Cmd": ([.*]),s*$/1/p # capture everything inside [...]
/^s*"Config": $/,/^s*$/ # only match next line inside "Config"
s/^s*"Image": "([^"]*)",s*$/1/p
s/^s*"Memory": ([0-9]*),s*$/1/p
This produces the output:
e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e
running
95168
0
fabric-lifecycle
false
1073741824
[ "lifecycle" ]
caspian/fabric:3.3.0.0-3594.2b8e92d
Wow..Thank you so much JigglyNaga ...Thanks for your patience ..
â Kalapriya Pasupuleti
Oct 4 at 12:34
add a comment |Â
up vote
0
down vote
I can't stress enough that sed
is an unsuitable tool for parsing arbitrary nested structures like JSON. But if you are forced down this path...
Simple match based on next entry name
Let's break down what your first sed
command is doing. It relies on matching the entry names of (hopefully) consecutive lines, and capturing what's between them:
.*"Privileged": (.*), "PublishAllPorts".*
matches these lines:
"Privileged": false,
"PublishAllPorts": false,
capturing the false
at the end of the first line.
You then changed that part into:
.*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*
That will no longer match, because the entry "Memory"
does not appear at that point in the file. If you want to match "Memory"
in the same manner as before, you will have to see what comes next. From your docker
output:
"Memory": 1073741824,
"NanoCpus": 0,
so you'll have to add this in the right place in your pattern:
"Memory": (.*), "NanoCpus"
All together, your sed
command will have to look like this:
sed -n `s/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "PublishAllPorts".*"Memory": (.*), "NanoCpus".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;8;9;7/p`
Note that 7
has to go at the end of the replacement, as you wanted "Memory" to appear last.
Stricter matching based on content
Rather than relying on the next entry name, you could capture only the contents that you expect for the entry, using stricter patterns than (.*)
. This still requires some context for the "Image" entry. I'm using the direct output from docker
, rather than saving into a variable and echo
, so the linebreaks are maintained. This means I also need some extra sed
trickery to join the "Cmd" back into a single line, and warrants putting the whole sed
command into a script.
sudo docker inspect fabric-lifecycle | sed -f extract.sed
Contents of extract.sed
:
#no echo
s/^s*"Id": "([^"]*)",s*$/1/p # anything but the closing "
s/^s*"Status": "([^"]*)",s*$/1/p
s/^s*"Pid": ([0-9]*),s*$/1/p # decimal number
s/^s*"ExitCode": ([0-9]*),s*$/1/p
s/^s*"Name": "/([^"]*)",s*$/1/p # match from "/ to closing "
s/^s*"Privileged": ([^,]*),s*$/1/p # match up to comma (unquoted boolean)
/^s*"Cmd": [/
:more
s/s+/ /g # collapse whitespace
/],s*$/bb # if closing ] found, jump to "final"
N # append next line
ba # loop to "more"
:final
s/^.*"Cmd": ([.*]),s*$/1/p # capture everything inside [...]
/^s*"Config": $/,/^s*$/ # only match next line inside "Config"
s/^s*"Image": "([^"]*)",s*$/1/p
s/^s*"Memory": ([0-9]*),s*$/1/p
This produces the output:
e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e
running
95168
0
fabric-lifecycle
false
1073741824
[ "lifecycle" ]
caspian/fabric:3.3.0.0-3594.2b8e92d
Wow..Thank you so much JigglyNaga ...Thanks for your patience ..
â Kalapriya Pasupuleti
Oct 4 at 12:34
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I can't stress enough that sed
is an unsuitable tool for parsing arbitrary nested structures like JSON. But if you are forced down this path...
Simple match based on next entry name
Let's break down what your first sed
command is doing. It relies on matching the entry names of (hopefully) consecutive lines, and capturing what's between them:
.*"Privileged": (.*), "PublishAllPorts".*
matches these lines:
"Privileged": false,
"PublishAllPorts": false,
capturing the false
at the end of the first line.
You then changed that part into:
.*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*
That will no longer match, because the entry "Memory"
does not appear at that point in the file. If you want to match "Memory"
in the same manner as before, you will have to see what comes next. From your docker
output:
"Memory": 1073741824,
"NanoCpus": 0,
so you'll have to add this in the right place in your pattern:
"Memory": (.*), "NanoCpus"
All together, your sed
command will have to look like this:
sed -n `s/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "PublishAllPorts".*"Memory": (.*), "NanoCpus".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;8;9;7/p`
Note that 7
has to go at the end of the replacement, as you wanted "Memory" to appear last.
Stricter matching based on content
Rather than relying on the next entry name, you could capture only the contents that you expect for the entry, using stricter patterns than (.*)
. This still requires some context for the "Image" entry. I'm using the direct output from docker
, rather than saving into a variable and echo
, so the linebreaks are maintained. This means I also need some extra sed
trickery to join the "Cmd" back into a single line, and warrants putting the whole sed
command into a script.
sudo docker inspect fabric-lifecycle | sed -f extract.sed
Contents of extract.sed
:
#no echo
s/^s*"Id": "([^"]*)",s*$/1/p # anything but the closing "
s/^s*"Status": "([^"]*)",s*$/1/p
s/^s*"Pid": ([0-9]*),s*$/1/p # decimal number
s/^s*"ExitCode": ([0-9]*),s*$/1/p
s/^s*"Name": "/([^"]*)",s*$/1/p # match from "/ to closing "
s/^s*"Privileged": ([^,]*),s*$/1/p # match up to comma (unquoted boolean)
/^s*"Cmd": [/
:more
s/s+/ /g # collapse whitespace
/],s*$/bb # if closing ] found, jump to "final"
N # append next line
ba # loop to "more"
:final
s/^.*"Cmd": ([.*]),s*$/1/p # capture everything inside [...]
/^s*"Config": $/,/^s*$/ # only match next line inside "Config"
s/^s*"Image": "([^"]*)",s*$/1/p
s/^s*"Memory": ([0-9]*),s*$/1/p
This produces the output:
e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e
running
95168
0
fabric-lifecycle
false
1073741824
[ "lifecycle" ]
caspian/fabric:3.3.0.0-3594.2b8e92d
I can't stress enough that sed
is an unsuitable tool for parsing arbitrary nested structures like JSON. But if you are forced down this path...
Simple match based on next entry name
Let's break down what your first sed
command is doing. It relies on matching the entry names of (hopefully) consecutive lines, and capturing what's between them:
.*"Privileged": (.*), "PublishAllPorts".*
matches these lines:
"Privileged": false,
"PublishAllPorts": false,
capturing the false
at the end of the first line.
You then changed that part into:
.*"Privileged": (.*), "Memory": (.*), "PublishAllPorts".*
That will no longer match, because the entry "Memory"
does not appear at that point in the file. If you want to match "Memory"
in the same manner as before, you will have to see what comes next. From your docker
output:
"Memory": 1073741824,
"NanoCpus": 0,
so you'll have to add this in the right place in your pattern:
"Memory": (.*), "NanoCpus"
All together, your sed
command will have to look like this:
sed -n `s/.*"Id": "(.*)", "Created".*"Status": "(.*)", "Running".*"Pid": (.*),.*"ExitCode": (.*), "Error".*"Name": "/(.*)", "RestartCount".*"Privileged": (.*), "PublishAllPorts".*"Memory": (.*), "NanoCpus".*"Cmd": (.*), "Image": "(.*)", "Volumes".*/1;2;3;4;5;6;8;9;7/p`
Note that 7
has to go at the end of the replacement, as you wanted "Memory" to appear last.
Stricter matching based on content
Rather than relying on the next entry name, you could capture only the contents that you expect for the entry, using stricter patterns than (.*)
. This still requires some context for the "Image" entry. I'm using the direct output from docker
, rather than saving into a variable and echo
, so the linebreaks are maintained. This means I also need some extra sed
trickery to join the "Cmd" back into a single line, and warrants putting the whole sed
command into a script.
sudo docker inspect fabric-lifecycle | sed -f extract.sed
Contents of extract.sed
:
#no echo
s/^s*"Id": "([^"]*)",s*$/1/p # anything but the closing "
s/^s*"Status": "([^"]*)",s*$/1/p
s/^s*"Pid": ([0-9]*),s*$/1/p # decimal number
s/^s*"ExitCode": ([0-9]*),s*$/1/p
s/^s*"Name": "/([^"]*)",s*$/1/p # match from "/ to closing "
s/^s*"Privileged": ([^,]*),s*$/1/p # match up to comma (unquoted boolean)
/^s*"Cmd": [/
:more
s/s+/ /g # collapse whitespace
/],s*$/bb # if closing ] found, jump to "final"
N # append next line
ba # loop to "more"
:final
s/^.*"Cmd": ([.*]),s*$/1/p # capture everything inside [...]
/^s*"Config": $/,/^s*$/ # only match next line inside "Config"
s/^s*"Image": "([^"]*)",s*$/1/p
s/^s*"Memory": ([0-9]*),s*$/1/p
This produces the output:
e5330efcb429bab6bb31571fb878d0cd3a7078c537bd9606360a05886e55f72e
running
95168
0
fabric-lifecycle
false
1073741824
[ "lifecycle" ]
caspian/fabric:3.3.0.0-3594.2b8e92d
answered Oct 4 at 12:23
JigglyNaga
3,062626
3,062626
Wow..Thank you so much JigglyNaga ...Thanks for your patience ..
â Kalapriya Pasupuleti
Oct 4 at 12:34
add a comment |Â
Wow..Thank you so much JigglyNaga ...Thanks for your patience ..
â Kalapriya Pasupuleti
Oct 4 at 12:34
Wow..Thank you so much JigglyNaga ...Thanks for your patience ..
â Kalapriya Pasupuleti
Oct 4 at 12:34
Wow..Thank you so much JigglyNaga ...Thanks for your patience ..
â Kalapriya Pasupuleti
Oct 4 at 12:34
add a comment |Â
Hello and welcome to StackExchange. It is very unclear what you're asking. Please edit the question to provide more details, for example the desired output.
â Panki
Oct 4 at 8:41
1
Also to deal with json it's usually easier to use json parser tool.
â Kiwy
Oct 4 at 11:30