2

I have file with json values :

abc={"adf": "def",   "abc2":"def2", "abc3" : "def3" }
abc4= 1
abc = {hello : world, hello:"a"}

I want to remove whitespace from everywhere in between { and } and not any other place. Values don't have any spaces in my case.

abc={"adf":"def","abc2":"def2","abc3":"def3"}
abc4= 1
abc = {hello:world,hello:"a"}

How can i use sed, awk or perl or any tool to achieve this ?

Note: All values are in one line only. There is no multi line processing required.

marked as duplicate by cas, Community Jan 3 '18 at 8:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • also a duplicate of several other questions, but Stéphane's answer is very good. – cas Jan 3 '18 at 8:25
2

Ugly Perl one-liner (works on Perl v5.24.1):

$ perl -pe 's/{.*?}/ $& =~ s, ,,gr /eg'  input
abc={"adf":"def","abc2":"def2","abc3":"def3"}
abc4= 1
abc = {hello:world,hello:"a"}

The outer substitution (s///) matches strings surrounded in braces, and replaces them by doing another substitution (s,,,) on the matched string.

Not the answer you're looking for? Browse other questions tagged or ask your own question.