kevzettler
kevzettler

Reputation: 5213

Sed multiple capture group substitution on one line of input

I have a file of JSON data that is all on a single line. The JSON has some file paths in that I need to update. Heres a sample.

"diffuseTexture":{"name":"Axe_03_diffuse.png","level":1.0,"hasAlpha":false,"getAlphaFromRGB":false,"coordinatesMode":0,"isCube":false,"uOffset":0.0,"vOffset":0.0,"uScale":1.0,"vScale":1.0,"uAng":0.0,"vAng":0.0,"wAng":0.0,"wrapU":1,"wrapV":1,"coordinatesIndex":0,"isRenderTarget":false,"renderTargetSize":0,"mirrorPlane":null,"renderList":null,"animations":[]},"diffuseFresnelParameters":null,"ambientTexture":null,"opacityTexture":null,"opacityFresnelParameters":null,"reflectionTexture":null,"reflectionFresnelParameters":null,"emissiveTexture":null,"emissiveFresnelParameters":null,"specularTexture":null,"bumpTexture":null},{"name":"hammer01","id":"dd00d71c-5865-4574-9437-e2777fef63f0","backFaceCulling":true,"wireframe":false,"ambient":[0.588,0.588,0.588],"diffuse":[1.0,1.0,1.0],"specular":[0.0,0.0,0.0],"emissive":[0.0,0.0,0.0],"specularPower":25.6,"alpha":1.0,"diffuseTexture":{"name":"Hammer_01_diffuse.png","level":1.0,"hasAlpha":false,"getAlphaFromRGB":false,"coordinatesMode":0,"isCube":false,"uOffset":0.0,"vOffset":0.0,"uScale":1.0,"vScale":1.0,"uAng":0.0,"vAng":0.0,"wAng":0.0,"wrapU":1,"wrapV":1,"coordinatesIndex":0,"isRenderTarget":false,"renderTargetSize":0,"mirrorPlane":null,"renderList":null,"animations":[]},"diffuseFresnelParameters":null,"ambientTexture":null,"opacityTexture":null,"opacityFresnelParameters":null,"reflectionTexture":null,"reflectionFresnelParameters":null,"emissiveTexture":null,"emissiveFresnelParameters":null,"specularTexture":null,"bumpTexture":null},{"name":"hammer02","id":"ecfa63fb-6939-4ca6-932c-4413693731dc","backFaceCulling":true,"wireframe":false,"ambient":[0.588,0.588,0.588],"diffuse":[1.0,1.0,1.0],"specular":[0.0,0.0,0.0],"emissive":[0.0,0.0,0.0],"specularPower":25.6,"alpha":1.0,"diffuseTexture":{"name":"Hammer_02_diffuse.png",

The part of the data that i'm trying to update are the .png references I want to prefix them with a further path so Axe_03_diffuse.png would become /assets/babylon/Axe_03_diffuse.png I'm having problems doing the replace because the file input is all on one line. The wild card matchers have been matching the whole input and not breaking properly.

I've been trying different permutations of:

cat ./hero.babylon | sed -E "s/(\"diffuseTexture\"\:\{\"name\"\:\")(..*)\.png/\1\/assets\/babylon\/\2/g"

This works fine until the (..*) wild card group.

Upvotes: 0

Views: 362

Answers (2)

anishsane
anishsane

Reputation: 20980

I guess, this could help:

sed -r 's|"name":"([^"]*.png)|"name":"/path/to/prefix/\1|g' test.json

Since you already know sed, I don't think, much explanation is required.

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174796

You could try the below sed command.

sed -r 's~(\"diffuseTexture\"\:\{\"name\"\:\")([^\"]*\.png)\"~\1/assets/babylon/\2"~g' file

[^"]* matches any char but not of double quotes, zero or more times.

Upvotes: 1

Related Questions