BlackChen'site

Shell 通过 grep 获取json文件中的值

json文件内容:

$ cat config.json
{"robotCode":"HX-00046","robotName":"hx-robot-00046","mallId":200,"mallName":"xxx","buildingId":0,"floorId":7,"mapName":"200_0_7"}

需要获取robotCode的值:

$ cat config.json | grep -oP '(?<="robotCode":")[^"]*'
HX-00046

匹配robotCode":"开头的, 多个[^"](非引号)字符.

grep:

-o --only-matching
    Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

-P, --perl-regexp
    Interpret PATTERN as a Perl  regular  expression. This  is  highly  experimental  and  grep  -P  may  warn  of unimplemented features.

评论