The UNIX and Linux Forums  

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
String substitution ctrl-alt-del Shell Programming and Scripting 3 10-14-2008 03:37 AM
string substitution laxmi Shell Programming and Scripting 3 02-16-2008 06:11 AM
String Substitution laxmi UNIX for Advanced & Expert Users 1 02-16-2008 06:08 AM
Sed - substitution for whole string Scarlos Shell Programming and Scripting 4 07-12-2005 08:56 AM
Sed String Substitution pciatto UNIX for Dummies Questions & Answers 2 04-29-2002 11:10 AM

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 12-05-2008
jacks jacks is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 6
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 (permalink)  
Old 12-05-2008
freelong freelong is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 38
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 (permalink)  
Old 12-05-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink 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 (permalink)  
Old 12-05-2008
freelong freelong is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 38
Here is another simple one:

Code:
sed -e 's/([^)]*)//g' 's/\|/;/g' filename

  #5 (permalink)  
Old 12-05-2008
jacks jacks is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 6
Thanks guys..That works fine..

In suggestion from the previous reply, I need to substitute in second step ";" for "|", but that's okay...
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 07:49 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