json文件内容:

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

需要获取robotCode的值:

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

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

grep:

1
2
3
4
5
-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.