Removing spaces between parenthesis ()

Hello,

i 've go a file with the following text:

oracle@das (J005)                                              0
oracle@das (J008)                                              0
oracle@das (J050)                                              0
oracle@das (J038)                                              0
oracle@das (J105)                                              0
oracle@das (J058)                                              0
oracle@das (J088)                                              0
oracle@das (J090)                                              0
oracle@das (J032)                                              1
oracle@das (J113)                                              0
oracle@das (J121)                                              0
httpd@MANAGED-UAS1 (TNS V1-V3)                                 0
oracle@das (J002)                                              0

I tried numerous ways with sed to remove only any space which happen to be in the parenthesis only ().
Such that:

httpd@MANAGED-UAS1 (TNS V1-V3)

Will have:

httpd@MANAGED-UAS1 (TNSV1-V3)

i've tried:

cat test.txt | sed -e 's/([(^)]*)//g'
 perl -p 's{(\( .*? \))}{$1 =~ y/ //dr}gex'
 sed -e 's/([(^)]*)( )/\1/'

But none seems to work. Any thoughts or hints?

Rgds,

H, try:

awk -F'[)(]' '{for(i=2; i<=NF; i+=2) {p=$i; gsub(/ /,x,p); sub($i,p)}}1' file

Can there be more than one space between the parentheses that have to be removed?
Is the position of the last field to be retained?

EDIT: howsoever, try

sed  ':L; s/\(([^ )]*\) \([^)]*)\)/\1\2 /; tL' file
1 Like
sed -r ':1;s/(\s)([^(]*\))/\2\1/;t1' file

--- Post updated at 03:23 ---
maybe only this works in Solaris

sed -r ':1;s/( )([^(]*\))/\2\1/;t1' file

Hi,

Thanks for your responses. Unfortunately, none of these worked. Forgot also to tell that the platform is Solaris 10 i86.

[PRODUCTION] root@das:/var/opt/Aspider-NGI/nms/output> awk -F'[)(]' '{for(i=2; i<=NF; i+=2) {p=$i; gsub(/ /,x,p); sub($i,p)}}1' test.txt
awk: syntax error near line 1
awk: illegal statement near line 1
[..]

Nearly correct. Try this:

perl -pe 's{(\( .*? \))}{$1 =~ y/ //dr}gex' file.txt

or a little shorter and maybe more robust:

perl -pe 's/(\([^)]+\))/$1 =~ s, ,,r/e;' file.txt

Perls feature use command in regex-replace seems to be quite useful in some situations.

1 Like

Hmmm - did you learn something from this post and, esp. its answer?

And, if you encounter a "Label too long:" message - what would you do to correct the error - adapt the label?

@RudiC: The link is broken.

1 Like

Thanks stomp! Fat fingers when copying the link. Corrected in above post...

Thank you all for your inputs!

Looking at little bit at Perl one-liner did the trick

perl -pe 's{(\([^\)]*\))}{($r=$1)=~s/ //g;$r}ge' test.txt
sed  -e ':L' -e 's/\(([^ )]*\) \([^)]*)\)/\1\2 /; tL' file

Like RudiC mentioned, on Solaris, use /usr/xpg4/bin/awk , not regular awk ...

1 Like

The following code works:

#!/bin/sh

# POSIX shell compliant.

grep -i "httpd" input.txt | sed "s/ //" | sed "s/ //" |sed "s/(/ (/"

exit 0
1 Like

Hi johnprogrammer,
Welcome to the UNIX and Linux forums.

Note that the problem being addressed in this thread didn't say anything about only wanting to remove spaces between parentheses on lines that also contain the string "httpd".

Note that the problem being addressed in this thread didn't say anything about removing lines that do not contain spaces between parentheses.

Note that the problem being addressed in this thread didn't say anything about only wanting to remove a single space between parentheses.

Note that the problem being addressed in this thread didn't say anything about only wanting to remove spaces between parentheses only lines that have exactly one <space> before the first parenthesis on that line.

Note that in post #1 in this thread, the desired sample output for the line that is to be changed also deletes a bunch of spaces and a number after the parentheses (which wasn't mentioned in the English description of the problem).

Please be careful about saying that your code works when it looks like the code that you have provided makes several assumptions that do not seem to match the requirements being addressed in this thread.

Hi nms,
I would also like to welcome you to the UNIX and Linux forums.

Please don't just say "Unfortunately, none of these worked."; please tell us where they fell short of doing what you wanted.

The requirements you stated in post #1 in this thread didn't make it clear whether or not lines that did not have a <space> between parentheses were supposed to be included in the output you are trying to produce or not.

The requirements you stated in post #1 in this thread didn't say anything about removing the <space>s and the "0" at the end of the line from the line you did want to change, but the sample output you provided removed those character as well as the <space> that was on that line between the parentheses.

In the future, please try to give a more complete description of exactly what it is that you are trying to do.

Thank you.

Those were what I understood.

Yes, I knew that, but I considered my answer as a good starting point.

This is important, and you are right. I thought a few times to edit the post, and write: "I think this should work". But unfortunately I didn't. Sorry.

Unix sed does not end a lable at a semicolon, needs a boundary:

sed  '
 :L
 s/\(([^ )]*\) \([^)]*)\)/\1\2 /
 tL
' file

or

sed  -e ':L' -e 's/\(([^ )]*\) \([^)]*)\)/\1\2 /; tL' file

And of course it does not take -r and ERE.

1 Like