0

I am trying to replace the string }{ with },{ using awk and gsub.

My attempt is:

cat blobs.txt | awk '{gsub(/\\}\\{/,"},{"); print}' >> blobsDone.txt

I have read somewhere that characters like brackets need two backslashes to get matched but is not working. Can someone help me with it? I am getting crazy.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
user2343163
  • 23
  • 1
  • 3

2 Answers2

2

This will do it directly

sed -i 's/}{/},{/g' blobsDone.txt

Looks for }{ and replaces by },{ any time it is found in blobsDone.txt. The file is updated with new content.

If you do not want the file to be updated, just delete the -i parameter.

Incase it is },{ to }{:

sed -i 's/},{/}{/g' blobsDone.txt
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • The OP wanted it the other way round `},{`-> `}{` so you might want to fix that ;-) but other than that I agree, the right tool here would be `sed`. – Adrian Frühwirth May 06 '13 at 12:38
  • It is funny because I focused on the title of the question. Then in the text he/she says the opposite and in the code it is like I do. I will ask him to know : ) Anyway, thanks for pointing this out! – fedorqui May 06 '13 at 12:41
  • Thanks for the fast reply! How do I specify the input file if not with cat? And then, I just realised that I have a space between the two brackets. How do I match it? – user2343163 May 06 '13 at 12:41
  • @user2343163, sed gets it. It is the same `cat file | sed something` than `sed something file`. – fedorqui May 06 '13 at 12:42
  • Sorry I was looking for this: }{-> },{ – user2343163 May 06 '13 at 12:44
  • @fedorqui I didn't even see that, apologies :-) – Adrian Frühwirth May 06 '13 at 12:45
  • No problem, @AdrianFrühwirth, it was good to fix this ambiguity. – fedorqui May 06 '13 at 12:48
  • Thank you very much guys! You were really kind! Do you also know how to match a whitespace? Because I just realised that I have a whitespace between the two brackets. I tried with sed -i 's/}\s{/},{/g' blobs.txt but it's not working yet. – user2343163 May 06 '13 at 12:55
  • In that case it would be `sed -i 's/} {/},{/g' blobsDone.txt`. If your question is answered, tick this answer or Ed Morton's so it is clear your problem is solved. – fedorqui May 06 '13 at 12:56
2
awk '{gsub(/}{/,"},{"); print}' blobs.txt >> blobsDone.txt

wrt to "I have read somewhere that characters like brackets need two backslashes" - google regular expressions and in particular regular expression metacharacters and get the book Effective Awk Programming, Third Edition By Arnold Robbins, http://www.oreilly.com/catalog/awkprog3/.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185