Removing spaces between parenthesis ()


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 2 Weeks Ago
Removing spaces between parenthesis ()

Hello,

i 've go a file with the following text:

Code:
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:
Code:
httpd@MANAGED-UAS1 (TNS V1-V3)

Will have:
Code:
httpd@MANAGED-UAS1 (TNSV1-V3)

i've tried:
Code:
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,
# 2  
Old 2 Weeks Ago
H, try:
Code:
awk -F'[)(]' '{for(i=2; i<=NF; i+=2) {p=$i; gsub(/ /,x,p); sub($i,p)}}1' file

# 3  
Old 2 Weeks Ago
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
Code:
sed  ':L; s/\(([^ )]*\) \([^)]*)\)/\1\2 /; tL' file


Last edited by RudiC; 2 Weeks Ago at 04:45 PM..
This User Gave Thanks to RudiC For This Post:
nezabudka (2 Weeks Ago)
# 4  
Old 2 Weeks Ago
Code:
sed -r ':1;s/(\s)([^(]*\))/\2\1/;t1' file

--- Post updated at 03:23 ---
maybe only this works in Solaris
Code:
sed -r ':1;s/( )([^(]*\))/\2\1/;t1' file

# 5  
Old 2 Weeks Ago
Hi,

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

Code:
[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
[..]


Last edited by Scrutinizer; 2 Weeks Ago at 05:24 PM..
# 6  
Old 2 Weeks Ago
Quote:
Code:
perl -p 's{(\( .*? \))}{$1 =~ y/ //dr}gex' file.txt

Nearly correct. Try this:

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

or a little shorter and maybe more robust:

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

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

Last edited by stomp; 2 Weeks Ago at 03:13 AM..
This User Gave Thanks to stomp For This Post:
nms (2 Weeks Ago)
# 7  
Old 2 Weeks Ago
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?

Last edited by RudiC; 2 Weeks Ago at 04:51 AM..
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Removing blanks, spaces sususa Shell Programming and Scripting 5 07-03-2012 03:57 AM
Removing blank spaces rogerben Solaris 6 01-30-2012 02:30 AM
awk for removing spaces shruthidwh Shell Programming and Scripting 4 05-16-2011 12:25 PM
removing spaces in filenames ianebaj Shell Programming and Scripting 4 11-05-2010 02:32 AM
Removing spaces from record raghavendra.cse Shell Programming and Scripting 3 04-08-2010 07:27 AM
Removing spaces within Filename Fishn Shell Programming and Scripting 7 10-21-2009 12:21 PM
Removing spaces in a line krishna_gnv Shell Programming and Scripting 2 09-20-2009 10:20 PM
Removing blank spaces, tab spaces from file NARESH1302 Shell Programming and Scripting 3 08-07-2009 11:18 AM
Removing spaces from string ippy98 Shell Programming and Scripting 7 03-18-2009 11:10 PM
Removing spaces from data monika Shell Programming and Scripting 4 11-09-2008 03:34 PM
Removing spaces... lost UNIX for Dummies Questions & Answers 2 10-06-2008 05:46 PM
removing spaces sahithi_khushi Shell Programming and Scripting 2 11-03-2006 10:20 AM
Removing spaces at particular position superprogrammer Shell Programming and Scripting 7 07-10-2006 05:29 AM
removing spaces after sperator vishnu_vaka Shell Programming and Scripting 6 03-24-2006 09:17 AM
Removing spaces between records thanuman UNIX for Dummies Questions & Answers 6 11-17-2005 12:01 PM