在每行

时间:2016-07-08 14:54:23

标签: regex bash perl awk sed

我有ndjson文件,字符串如下:

{"created": "2016-03-08 00:00:00 UTC", "changed": "2016-03-08 08:51:56 UTC", "rev": 28990, "status": 1, "user": [{"user_id": null, "name": null, "loyaltyCard": 123456789012}], "id": "26680533564", "tax": null, "products": [{"price": 289, "quantity": 1, "coupon": null, "id": "4122"}],  "shipping": 0.0}

我需要接受“loyaltyCard”的双引号值,考虑到它可能是数字,字母(也是西里尔字母)和其他任何东西。

期待看到类似的东西:

UTC", "rev": 280, "status": 1, "user": [{"user_id": null, "name": null, "loyaltyCard": "123456789012"}], "id": "26680533564", "tax": null, "products": [{"price": 289, "quantity": 1, "coupon": null, "id": "4122"}],  "shipping": 0.0}
UTC", "rev": 56990, "status": 1, "user": [{"user_id": 543445, "name": null, "loyaltyCard": "1233bla456bla"}], "id": "5454580534", "tax": null, "products": [{"price": 869, "quantity": 2, "coupon": null, "id": "86854"}],  "shipping": 0.0}

3 个答案:

答案 0 :(得分:1)

如果你想用双引号括起loyaltyCard值,你可以使用这样的正则表达式:

(loyaltyCard": )([^}]*)\}

使用替换字符串:

\1"\2"}

<强> Regex demo

Vim的

更新:要在vim中查找/替换,您可以使用:

:%s/\(loyaltyCard": \)\([^}]*\)\}/\1"\2"}/
 ^ ^----------------------------^ ^-----^
 | + Pattern1 (w/capture groups)  + Pattern 2 (w/ group refs)
 + substitute cmd

enter image description here

答案 1 :(得分:0)

s/: (\d+)/: "$1"/ might do the trick

答案 2 :(得分:0)

这是其中一种方式:

 m/"loyaltyCard"\:\s*"([^"]*)"/g;

如果您想根据自己的意愿提供转义序列。