substitution of string in brackets


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting substitution of string in brackets
# 1  
Old 12-05-2008
substitution of string in brackets

Hi friends!
I have a tab delimited file with two columns :
GB_45_DRB SP:0139466(mrmi sisignm)|SP:3674(fllflg_itoioh)|SP:68954779(RMTKLGF to emmdm-roomto)
GB_45_DRD SP:475928(mgmdksi rikgkg)|SP:587959(roykgl tiic-tm)|SP:0139466(mrmi sisignm)|SP:3674(fllflg_itoioh)|SP:68954779(RMTKLGF to emmdm-roomto)
GB_45_DRCD
GB_45_P5294 SP:3735599(fkkfdlk)

I want to remove all occurences of stuff between brackets(inclusive of brackets) in second column and replace all "|" with ";"

So that it looks like SP:0139466;SP:3674;SP:68954779 in second col. Problem is that second col might be empty, as in third row here.
Ur help would be appraciated..
Thanks

Last edited by jacks; 12-05-2008 at 01:25 PM..
# 2  
Old 12-05-2008
Try this one:
Code:
awk -F"[()]" '{
     gsub(/\|/, ";", $0);
     n=split($0, arr, /[()]/);}
     {for (i=1; i<=n; i++)
        if(i%2 == 1)
                printf ("%s", $i)
      }
      {print "";}
        ' yourfilename

The output I got is:
Code:
GB_45_DRB       SP:0139466;SP:3674;SP:68954779
GB_45_DRD       SP:475928;SP:587959;SP:0139466;SP:3674;SP:68954779
GB_45_DRCD
GB_45_P5294      SP:3735599

# 3  
Old 12-05-2008
Hammer & Screwdriver Here is a possible solution

The first example below does the 'eliminate (stuff)' and the second also does the substitution of delimiters.
I thought there would have been a way to use [[:print:]] or [[:graph:]] to select the letters and sumbols inside the (), but I could not get that to work properly.

Code:
> echo "GB_45_DRB SP:0139466(mrmi sisignm)|SP:3674(fllflg_itoioh)|SP:68954779(RMTKLGF to emmdm-roomto)" | sed "s/([A-Za-z _\-]*)//g"
GB_45_DRB SP:0139466|SP:3674|SP:68954779

> echo "GB_45_DRB SP:0139466(mrmi sisignm)|SP:3674(fllflg_itoioh)|SP:68954779(RMTKLGF to emmdm-roomto)" | sed "s/([A-Za-z _\-]*)//g" | tr "|" ";"
GB_45_DRB SP:0139466;SP:3674;SP:68954779

>

# 4  
Old 12-05-2008
Here is another simple one:
Code:
sed -e 's/([^)]*)//g' 's/\|/;/g' filename

# 5  
Old 12-05-2008
Thanks guys..That works fine..

In suggestion from the previous reply, I need to substitute in second step ";" for "|", but that's okay...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue catching/reading 2 digits and 3 letter string between brackets

Hello I'm writing a handler for ffmpeg, and having troubles to catch some exceptions that may occour with certain files. In order to parse for video & subtitle maps, i've had to make the raw data easier to handle, until now this worked well, but basicly i've just been lucky... The input... (1 Reply)
Discussion started by: sea
1 Replies

2. UNIX for Advanced & Expert Users

grep string containing [] brackets

Hello, I am trying to grep string with square brackets. for example I want to grep the below string in log.txt file. This is a test thanks in advance. (2 Replies)
Discussion started by: sureshcisco
2 Replies

3. Shell Programming and Scripting

How to substitute brackets in the beginning of string in perl?

Hi, I have a string like this user can specify different query sets that is why "or" is mentioned: $string="]("; or $string="](("; or $string="]((("; or $string="]((((("; (1 Reply)
Discussion started by: vanitham
1 Replies

4. Shell Programming and Scripting

Deleting part of a string enclosed in brackets

I use otool on OS X to figure out the shared libraries that a binary uses. I run this command: otool -L /Applications/Vidnik\ 0.13.0/Vidnik.app/Contents/MacOS/Vidnik And it returns an output similar to this: /Applications/Vidnik 0.13.0/Vidnik.app/Contents/MacOS/Vidnik:... (10 Replies)
Discussion started by: pcwiz
10 Replies

5. Shell Programming and Scripting

string substitution in perl

Hi, I have a template file and want to replace 3 parameters to the values that I want. these values are in a parameter file. Any idea how to do this in perl? the parameter file looks like: host_name = jupiter PORT = 1562 IPADDRESS = 10.1.34.10 the template file has lots of entry.... (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

6. Shell Programming and Scripting

String substitution

Hi, I have a properties file (myprop.properties) which contains some values: @oneValue@==tcp://localhost:1234 @twoValue@==tcp://localhost:4563 @threeValue@==tcp://localhost7895 I have a xml file (myXmlFile.xml)which contains some tokens: <application name="aTest"> <NameValuePair> ... (3 Replies)
Discussion started by: ctrl-alt-del
3 Replies

7. Shell Programming and Scripting

string substitution

Hey ppl, Could u tell me how to replace such a string xyz->x with XYZ(x), where x can be any variable accessible by pointer to structure, xyz in an entire file? (3 Replies)
Discussion started by: laxmi
3 Replies

8. UNIX for Advanced & Expert Users

String Substitution

Hey ppl, Could u tell me how to replace such a string xyz->x with XYZ(x), where x can be any variable accessible by pointer to structure, xyz in an entire file? (1 Reply)
Discussion started by: laxmi
1 Replies

9. Shell Programming and Scripting

String Substitution Question

When I run the script I pass in 2 expressions (ex. replace.ksh new old) I want the script to go line by line for a given file in a given directory and replace the word new with old. Of course in my line where I have the awk statement it is replacing the 2nd word with 1st instead of new with... (3 Replies)
Discussion started by: goodrics
3 Replies

10. UNIX for Dummies Questions & Answers

Sed String Substitution

Hi! I've the following script code with an input parameter: sed 's/oldstring/$1/g' myfile > newfile (I launch it with comman line: $ MyShell newstring) Problem: the substituion doesn't work (oldstring becomes $1, instead of newstring). How could I solve this situation? Thanks, ... (2 Replies)
Discussion started by: pciatto
2 Replies
Login or Register to Ask a Question