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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove newline, tab, spaces in curly braces.. :( Pls Help?
# 1  
Old 10-01-2014
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.

Code:
xyz (Exception e) { 

}

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

My desired output should be

Code:
xyz (Exception e) {}

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

Reg,
NY

Last edited by Don Cragun; 10-01-2014 at 05:28 AM.. Reason: Add CODE tags.
# 2  
Old 10-01-2014
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:
Code:
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:
Code:
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:
Code:
xyz (Exception e) {}

Free text

abc (int c) {}
free text 2

single (line test) {}
after single line

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

1.
Code:
xyz (Exception) {
                                                                                                         }

2.
Code:
xyz (Exception) {


                                                  }

3.
Code:
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,

Code:
xyz (Exception) {}

Moderator's Comments:
Mod Comment Please use CODE tags for sample input, sample output, and code segments as required by forum rules.


Thank you so much.

Last edited by Don Cragun; 10-01-2014 at 06:48 AM.. Reason: Add CODE tags.
# 4  
Old 10-01-2014
So, apparently you want something more like:
Code:
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:
Code:
xyz (Exception e) {}

Free text

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

single (line test) {}
after single line

# 5  
Old 10-01-2014
How about
Code:
awk     '/{/    {while (!(/}/)) {getline X; $0 = $0 X}
                 sub (/{[       ]*/,"{")
                 sub (/[        ]*}/, "}")
                }
         1
        ' file
xyz (Exception) {}


xyz (Exception) {}



xyz (Exception) {filhfs ijsdlfh sdlfhl}

# 6  
Old 10-01-2014
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
# 7  
Old 10-01-2014
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):
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check string end with curly braces

file.txt apple apples{ applepicture apple9 apple cake{ abple apple_and_cake appleapple apple apple( and my script while read line; do if ]; then echo "$line" fi done <file.txt read (10 Replies)
Discussion started by: cmdcmd
10 Replies

2. Shell Programming and Scripting

When curly braces needed?

Hello, i was trying to find get a command to list duplicated files so i tried ls dir1 dir2 | awk '{x++}' and it didnt work. After a bit of searching online i found that it works without the curly braces ls dir1 dir2 | awk 'x++' I thought the curly braces were needed in awk so... (6 Replies)
Discussion started by: andy391791
6 Replies

3. Shell Programming and Scripting

Curly braces in sed

Hi, I have below command in one of the script. Can you please let me know what does the curly braces do over here \{1,\}. The remaining part of the code atleast I am able to understand. sed -n 's/.*\-\()\{1,\}\)\-.*/\1/p' (13 Replies)
Discussion started by: tostay2003
13 Replies

4. Shell Programming and Scripting

** EMERGENCY ** Having trouble with 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... (1 Reply)
Discussion started by: NY_777
1 Replies

5. UNIX for Dummies Questions & Answers

How do I pull the value between curly braces?

Hi everyone, I've got a file that looks like this: uid{508}pid{22224}pname{/PPROGRAM/pprgramx -profile:LIVE -serv:as ... I want to pull the value of pid between the curly braces, or 22224 in this example. pid is always the second pair of curly braces, but the length of the number is... (7 Replies)
Discussion started by: Scottie1954
7 Replies

6. Shell Programming and Scripting

tar --exclude with curly braces

I'm having trouble understanding the exclude option in tar. From some web sites, it seems one is able to exclude several strings by enclosing them in curly brackets. However it seems to be "random" what gets excluded when using the curlies. I've been using the exclude-from=myfile option in a... (12 Replies)
Discussion started by: majest
12 Replies

7. Shell Programming and Scripting

sed in windows does not parse curly braces

Hi everyone: I'm stuck at this point, could you guys please give me some hints about what I am doing wrong in the following script, I'm using sed for windows: sed ^"$ {^ a^ STRINGTABLE DISCARDABLE^ BEGIN^ #define CLIENT_MODULE, "%CLIENT_MODULE%"^ #define CLIENT_ID, "%CLIENT_ID%"^... (1 Reply)
Discussion started by: edgarvm
1 Replies

8. Shell Programming and Scripting

find -regex not recognizing curly braces

Must be a bug or something. Whether I escape them or not, it will not work. No matter what I set the minimum and maximum to nothing gets caught. For instance: find / -regex "/.{0, 50}.*" -maxdepth 1 or find / -regex "/.\{0, 50\}.*" -maxdepth 1 should pretty much catch everything residing within... (4 Replies)
Discussion started by: stevensw
4 Replies

9. Shell Programming and Scripting

Curly braces assigned to variables

Hi, Im pretty new to Unix. I came across a script which was using PLSQL inside a script and there was an unusual thing mentioned. there was a variable assigned as P_CUR=${1} and one more as V_TAGFILE="$1" Couldnt find the difference. Also the variables were used in PLSQL... (1 Reply)
Discussion started by: njks68
1 Replies

10. Shell Programming and Scripting

Use of curly braces with variables

Hi, I am new to shell scripting.I have worked somewhat with Perl though. I am not able to find what the second line does and how does it do. <code> FP_RUNNING=`service filepool status` FP_RUNNING=${FP_RUNNING%% *} <\code> After the first line,the variable FP_RUNNING stores '1 FilePool... (2 Replies)
Discussion started by: abhinavsinha
2 Replies
Login or Register to Ask a Question