The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Parentheses in perl find/replace Jeffish Shell Programming and Scripting 3 08-21-2007 11:10 AM
Find and replace text bobo UNIX for Dummies Questions & Answers 1 01-12-2006 01:17 PM
Replace Text deep.singh UNIX for Dummies Questions & Answers 2 05-31-2005 12:03 PM
Need to replace text in an awk cdunavent Shell Programming and Scripting 2 05-10-2005 11:01 AM
Replace Text with sed networkfre@k Shell Programming and Scripting 5 11-24-2004 05:52 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-20-2008
lalelle lalelle is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 43
Replace text in parentheses

Hi

I would like to replace a comma in parentheses to a semicolon for example. Other commas outside () stay unchanged. How can I do this?

aaaa,bbb,ccc,ddd(eee,fff,ggg),hhh,iii
to
aaaa,bbb,ccc,ddd(eee;fff;ggg),hhh,iii

Thanks
  #2 (permalink)  
Old 02-20-2008
bobbygsk bobbygsk is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 90
Hope this works for you.
Quote:
awk -F"," '{print $1,",",$2,",",$3,",",$4,";",$5,";",$6,",",$7,",",$8}' file1 | sed 's/ //g'
sed is used to avoid spaces between "," (Commas)
  #3 (permalink)  
Old 02-20-2008
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
  
 

Join Date: Apr 2002
Location: Italy
Posts: 370
If in the string there is only a couple of open/closed parenthesis:

Code:
echo "aaaa,bbb,ccc,ddd(eee,fff,ggg),hhh,iii" | \
awk -F'[()]' '{ gsub(",",";",$2); print($1"("$2")"$3) }'
This is a rapid solution. For sure there will be a more clever and general method to obtain the same
  #4 (permalink)  
Old 02-20-2008
lalelle lalelle is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 43
Thanks a lot
The text in file can be dynamic.
aa,bb,cc
aa,bb,cc,dd,dfas,(dfd,d)df,df,afd
dafk
fads,df,dafa,dfa
a,d,d,f,g(1,1)fd
  #5 (permalink)  
Old 02-20-2008
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
  
 

Join Date: Apr 2002
Location: Italy
Posts: 370
Another quick and dirt one:
Code:
awk '{
   s="";
   replace=0;
   for (i=1; i<=length($0); i++) {
      c=substr($0,i,1);
      if (c == "(") replace=1;
      if (c == ")") replace=0;
      if (c == "," && replace) { s=s";" } else { s=s c };
   }
   print s;
}' input_file
However, it doesn't work if you have unmatched parenthesis on lines!
  #6 (permalink)  
Old 02-20-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,079
awk string function

Hi,

We can use awk string function GSUB to address this issue.

code:

Code:
nawk 'BEGIN{FS="[()]"}
{
gsub(/,/,";",$2)
printf("%s(%s)%s\n", $1,$2,$3)
}' file
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0