0

Say I have a text file like this:

a;bc;d;{a;b;cd}
ab;cde;f;{ab;c;defg}
ab;{a;b;cd};cde;f
...

and I want to replace all the semicolons in curly brackets by comma. It will look like this after substitution:

a;bc;d;{a,b,cd}
ab;cde;f;{ab,c,defg}
ab;{a,b,cd};cde;f
...

How should I do it in shell command? sed, awk or whatever...

1
  • Are the curly brackets well-balanced or nested? Commented Aug 26, 2014 at 14:19

3 Answers 3

5

Perl to the rescue:

perl -pe 's/({.*?})/ $1 =~ s=;=,=gr /ge' input

The problem is your expected output is wrong:

a;b;cd         a;b;cd
  |               |
  V               V
a,b,c,d         ab,cd
Sign up to request clarification or add additional context in comments.

Comments

4

Through perl which uses positive lookahead,

$ perl -pe 's/;(?=[^{}]*})/,/g' file
a;bc;d;{a,b,cd}
ab;cde;f;{ab,c,defg}
ab;{a,b,cd};cde;f

Comments

0
sed ':a; s/\(.*\)\({\)\([^;]*\)\(;\)\([^}]*}.*\)/\1\2\3,\5/;t a' file

put a label in the beginning and section off hold patterns and loops around until no more successful substitutions on each line (t a)

E.g.  
a;bc;d;{a;b;cd}morestuff{bsdj;dsfkjl;sdkjl;kd}
ab;cde;f;{ab;c;defg}
ab;{a;b;cd};cde;f

Output: 
a;bc;d;{a,b,cd}morestuff{bsdj,dsfkjl,sdkjl,kd}
ab;cde;f;{ab,c,defg}
ab;{a,b,cd};cde;f

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.