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.

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

1 Answers1

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.

ilkkachu
  • 138,973