Replace text in parentheses


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace text in parentheses
# 1  
Old 02-20-2008
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  
Old 02-20-2008
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  
Old 02-20-2008
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 Smilie
# 4  
Old 02-20-2008
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  
Old 02-20-2008
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  
Old 02-20-2008
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Answers to Frequently Asked Questions

Add string to parentheses

Suppose I have this code : int main () { int i = NULL; /* incorrect */ return 0; } and I want to put the word between the two parentheses like this : int main (void) { int i = NULL; /* incorrect */ return 0; } which command is used to do it in Linux ? (2 Replies)
Discussion started by: steve120
2 Replies

2. Shell Programming and Scripting

[All variants] remove first pair of parentheses

How to remove first pair of parentheses and content in them from the beginning of the line? Here's the list: (ok)-test (ok)-test-(ing) (some)-test-(ing)-test test-(ing) Desired result: test test-(ing) test-(ing)-test test-(ing) Here's what I already tried with GNU sed: sed -e... (6 Replies)
Discussion started by: useretail
6 Replies

3. Shell Programming and Scripting

Help with extracting data within parentheses

This is my input file: a|b|c(ef)|g|h(km)|p My output file should look like: a|b|ef|g|km|p That is, pipe is the delimiter. The data within pipe must be displayed as it is but if it encounters any data within parentheses, then only the data within parentheses has to be displayed ( the data... (2 Replies)
Discussion started by: ksatish89
2 Replies

4. Shell Programming and Scripting

Sed command to replace with pattern except for text and closing parentheses

Can someone help me with a sed command: There will be multiple occurences in a file that look like this: MyFunction(12c34r5) and I need to replace that with just the 12c34r5 for every occurrence. The text between the parentheses will be different on each occurrence, so I can't search for that.... (4 Replies)
Discussion started by: missb
4 Replies

5. Shell Programming and Scripting

When does an if statement need parentheses

I was looking at a script in my little book on bash and saw that one of the if statements had parentheses instead of brackets for the condition. I've been trying to find in my book where it talks about parentheses (because the examples on the if statement in an earlier chapter doesn't seem to... (3 Replies)
Discussion started by: Straitsfan
3 Replies

6. Shell Programming and Scripting

Find and add/replace text in text files

Hi. I would like to have experts help on below action. I have text files in which page nubmers exists in form like PAGE : 1 PAGE : 2 PAGE : 3 and so on there is other text too. I would like to know is it possible to check the last occurance of Page... (6 Replies)
Discussion started by: lodhi1978
6 Replies

7. UNIX for Dummies Questions & Answers

Replace all occurrences of strings with parentheses

Hi, I tried to adapt bartus's solution to my problem, without success. I want to replace all the occurences of this: with: , where something can contain an arbitrary number of balanced parens and brakets. Any ideas ? Best, (1 Reply)
Discussion started by: ff1969ff1969
1 Replies

8. Shell Programming and Scripting

How to replace text in a file with text entered

I am trying to write a shell script that will allow the typing of a value, then using that value to replace data in a text file. I suspect I need sed. The format of the file is: Variable1:Value1 Variable2:Value2 The interaction would be something like: Shell Prompt: "Please enter the... (9 Replies)
Discussion started by: cleanden
9 Replies

9. UNIX for Dummies Questions & Answers

search and replace a specific text in text file?

I have a text file with following content (3 lines) filename : output.txt first line:12/12/2008 second line:12/12/2008 third line:Y I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before. I tried using cat output.txt... (4 Replies)
Discussion started by: santosham
4 Replies

10. Shell Programming and Scripting

Parentheses in perl find/replace

I'm trying to use the following command to do a batch find and replace in all commonly named files through a file hierarchy find . -name 'file' |xargs perl -pi -e 's/find/replace/g' which works fine except for a substitution involving parenthesis. As a specific example I'm trying to sub... (3 Replies)
Discussion started by: Jeffish
3 Replies
Login or Register to Ask a Question