Blasterdick Blasterdick - 1 year ago 87
Perl Question

Put in double quotes all between two patterns in every line

I have ndjson file, with strings like this:

{"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}

I need to take in double quotes values of "loyaltyCard", considering, that it could be digits, letters (cyrillic also) and anything else.

Expecting to see something like:

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}

Answer Source

If you want to wrap up loyaltyCard value in double quotes, you can use a regex like this:

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

With a replacement string:


Regex demo

Update for Vim: to find/replace in vim you can use:

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

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download