How to remove newline, tab, spaces in curly braces.. :( Pls Help?

Hi Everyone,

in the below "xyz (Exception e)" part... after the curly braces, there is a new line and immediately few tabs are present before closing curly brace.

xyz (Exception e) { 

}

note: there can be one or more newlines between the curly braces.

My desired output should be

xyz (Exception e) {}

Have tried a lot using awk, sed etc.. but no use. Please help asap. Thank you so much.

Reg,
NY

There are a lot of things that aren't specified in your requirements. Making lots of wild assumptions, the following might be something you can use:

awk '
/{/,/}/ {
	if(sub("{.*$", "{}")) print
	next
}
1' file

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .

If file contains:

xyz (Exception e) { 

}

Free text

abc (int c) {
	garbage
	garbage
	garbage
		}
free text 2

single (line test) {     	}
after single line

the above code produces the output:

xyz (Exception e) {}

Free text

abc (int c) {}
free text 2

single (line test) {}
after single line

Thank yo Mr. Cragun for the fast reply. Let me explain you completely.. My file contain multiple exceptions. For instance, see the below 3.

xyz (Exception) {
                                                                                                         }
xyz (Exception) {


                                                  }
xyz (Exception) { 
                                            filhfs ijsdlfh sdlfhl }

In the above three different scenarios, no change required for 3rd scenario as there is data between curly braces.

I need the 1st & 2nd scenario to be handled as there is no data between curly braces and also, if there is space/tab exists in between the braces then it should be removed..
The output for the both 1st and 2nd scenarios should be same. I.e,

xyz (Exception) {}

Thank you so much.

So, apparently you want something more like:

awk '
/{/,/}/ {
	if(/{/)	o = $0
	else	o = o "\n" $0
	if(/}/)	{
		sub(/{[ \t\n]*}/, "{}", o)
		print o
	}
	next
}
1' file

which with the input I showed earlier,produces the output:

xyz (Exception e) {}

Free text

abc (int c) {
	garbage
	garbage
	garbage
		}
free text 2

single (line test) {}
after single line

How about

awk     '/{/    {while (!(/}/)) {getline X; $0 = $0 X}
                 sub (/{[       ]*/,"{")
                 sub (/[        ]*}/, "}")
                }
         1
        ' file
xyz (Exception) {}


xyz (Exception) {}



xyz (Exception) {filhfs ijsdlfh sdlfhl}

Thank you again for a so quick reply Mr. Cargun.

The solution given is not working as expected. For the better clarity, I am attaching the input and the desire output.

Please go through them once. Thank you so much.

regards,
NY

The "solution given is not working as expected" because you posted an inappropriate, truncated sample, and the files you are using are not NIX standard text files (containing extra win <CR> line terminating chars).
However, try (based on Don Cragun's proposal):

awk     '/([Ee]xcept.*) {/      {SV = $0; L=1; next}
         !L
         L                      {SV = SV RS $0
                                 if (/}/) {sub (/{[ \t\r\n]*}/, "{}", SV)
                                           print SV
                                           L=0
                                          }
                                }
        ' input.txt