-2

Text record:

(1,2,3,4,{fred,don,max,rat,grp},45,67,mat,jhon,{a,b,1,2,sd[{1,2},{4,5}],45,67,P[{34,56,34},{uni,cast,r}],c{q,ew,3,4},1,2,3,cf{2,4,5,8},6},4,fr{24,45,67},5,NOL)

Desired output:

(1,2,3,4,{fred:don:max:rat:grp},45,67,mat,jhon,{a:b:1:2:sd[{1:2}:{4:5}]:45:67:P[{34:56:34}:{uni:cast:r}]:c{q:ew:3:4}:1:2:3:cf{2:4:5:8}:6},4,fr{24:45:67},5,NOL)

needs to be replace by : for the below:

  1. Everything between {} and []
  2. The data has nested structure like {{}} and {[],[]}
  3. The text record is always between ().

Any help with perl is appreciated.

showdev
  • 28,454
  • 37
  • 55
  • 73
yuvi
  • 21
  • 3

1 Answers1

-1

You may try the below perl command which uses positive lookahead based regex.

perl -pe 's/,(?=(?:\{[^{}]*\}|[^{}])*})|,(?=(?:\[[^\[\]]*\]|[^\[\]])*\])/:/g' file

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Thanks Avinash...One issue is that the , is not replaced by : for a,b,1,2...........{a,b,1,2,sd[{1:2}:{4:5}],45,67,P[{34:56:34}:{uni:cast:r}],c{q:ew:3:4},1,2,3,cf{2:4:5:8}:6},4,fr{24:45:67} – yuvi Jul 13 '15 at 10:59
  • Thanks Avinash, this works. I will load this for the complete data set. – yuvi Jul 13 '15 at 11:13
  • 1
    May i know the reason for downvotes.. – Avinash Raj Jul 13 '15 at 11:56
  • 1
    I guess the downvotes come because you're helping someone who has shown absolutely no effort. I considered downvoting this answer (but I didn't). – Dave Cross Jul 13 '15 at 11:57
  • Even high rep users also answereing these type of no effort question. We can't stop them unless SO takes some action on closing these type of questions immediately as unclear or too-broad (like dupe-hammer). – Avinash Raj Jul 13 '15 at 12:06
  • Apologies, i was checking via a handheld & i dont remember down voting. I ve corrected by upgrading but still shows -1. Will check with admin tomorrow. – yuvi Jul 14 '15 at 18:01