例如,下面是我的程序的输出(bspwm,如果您想知道的话):
{
"id": 29360131,
"splitType": "vertical",
"splitRatio": 0.5,
"birthRotation": 90,
"vacant": true,
"sticky": false,
"private": false,
"locked": false,
"presel": null,
"rectangle": {
"x": 0,
"y": 0,
"width": 1920,
"height": 1200
},
"firstChild": null,
"secondChild": null,
"client": {
"className": "Termite",
"instanceName": "termite",
"borderWidth": 1,
"state": "floating",
"lastState": "tiled",
"layer": "normal",
"lastLayer": "normal",
"urgent": false,
"visible": true,
"icccmFocus": true,
"icccmInput": true,
"minWidth": 10,
"maxWidth": 0,
"minHeight": 19,
"maxHeight": 0,
"wmStatesCount": 0,
"wmState": [],
"tiledRectangle": {
"x": 0,
"y": 0,
"width": 958,
"height": 1198
},
"floatingRectangle": {
"x": 638,
"y": 394,
"width": 642,
"height": 410
}
}
}我想看看"state"是不是"tiling"。在这种情况下,它是"floating"。
发布于 2016-02-24 06:25:57
更好的选择是使用JSON解析器。
如果您坚持使用grep:
假设您的grep支持PCRE (-P):
bspwm | grep -Po '"state":\K[^,]*'这将得到键"state"的值(带有引号)。
如果您不希望周围的引号围绕键:
bspwm | grep -Po '"state":"\K[^"]*'例如:
% grep -Po '"state":\K[^,]*' <<<'{"id":29360131,"splitType":"vertical","splitRatio":0.500000,"birthRotation":90,"vacant":true,"sticky":false,"private":false,"locked":false,"presel":null,"rectangle":{"x":0,"y":0,"width":1920,"height":1200},"firstChild":null,"secondChild":null,"client":{"className":"Termite","instanceName":"termite","borderWidth":1,"state":"floating","lastState":"tiled","layer":"normal","lastLayer":"normal","urgent":false,"visible":true,"icccmFocus":true,"icccmInput":true,"minWidth":10,"maxWidth":0,"minHeight":19,"maxHeight":0,"wmStatesCount":0,"wmState":[],"tiledRectangle":{"x":0,"y":0,"width":958,"height":1198},"floatingRectangle":{"x":638,"y":394,"width":642,"height":410}}'
"floating"
% grep -Po '"state":"\K[^"]*' <<<'{"id":29360131,"splitType":"vertical","splitRatio":0.500000,"birthRotation":90,"vacant":true,"sticky":false,"private":false,"locked":false,"presel":null,"rectangle":{"x":0,"y":0,"width":1920,"height":1200},"firstChild":null,"secondChild":null,"client":{"className":"Termite","instanceName":"termite","borderWidth":1,"state":"floating","lastState":"tiled","layer":"normal","lastLayer":"normal","urgent":false,"visible":true,"icccmFocus":true,"icccmInput":true,"minWidth":10,"maxWidth":0,"minHeight":19,"maxHeight":0,"wmStatesCount":0,"wmState":[],"tiledRectangle":{"x":0,"y":0,"width":958,"height":1198},"floatingRectangle":{"x":638,"y":394,"width":642,"height":410}}'
floating发布于 2016-02-24 06:38:00
使用像吉顺这样的专用JSON解析器是一种更健壮的方法:
jshon -e client -e state -u < file
floatinghttps://unix.stackexchange.com/questions/265412
复制相似问题