在字符串中间的Grep通配符

时间:2013-07-11 12:53:31

标签: grep wildcard

好吧我遇到的问题是我有一个包含许多看起来像这样的行的文件。

  

{“cameraEndpoint”:“your.url.here”,“cameraId”:“20MP S Middle Port 2”,“warehouseId”:“random”}

Thig是我唯一需要的是实际的cameraId我尝试了一些grep fu而没有运气这是我跑的命令之一。

grep -o '\"cameraId\":\"*"' camlist.txt

但这只会返回

  

“cameraId”:”   “cameraId”:”   “cameraId”:”   “cameraId”:”   “cameraId”:”   “cameraId”:”   “cameraId”:”   “cameraId”:“

我实际上需要报价之间的数据。

1 个答案:

答案 0 :(得分:2)

如果我理解正确,您希望cameraId的属性为20MP S Middle Port 2

如果是这样,你可以这样做:

$ grep -Po '(?<=cameraId":")[^"]*' file
20MP S Middle Port 2

它会将cameraId":"之后的所有数据提取到下一个"

测试

$ cat file
{"cameraEndpoint":"your.url.here","cameraId":"20MP S Middle Port 2","warehouseId":"random"}
{"cameraEndpoint":"your.url.here","cameraId":"Hello S Middle Port 2","warehouseId":"random"}
{"cameraEndpoint":"your.url.here","camaId":"Hello S Middle Port 2","warehouseId":"random"}
$ grep -Po '(?<=cameraId":")[^"]*' file
20MP S Middle Port 2
Hello S Middle Port 2