The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search, replace string in file1 with string from (lookup table) file2? gstuart Shell Programming and Scripting 2 04-11-2008 11:32 AM
SED replace string by occurrence uttamhoode Shell Programming and Scripting 4 03-05-2008 02:04 AM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 07:24 AM
String Search & Replace IwishIknewC UNIX for Dummies Questions & Answers 1 03-25-2006 03:28 AM
string search replace krishna UNIX for Advanced & Expert Users 1 12-19-2001 10:49 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-02-2006
Registered User
 

Join Date: May 2006
Posts: 5
Search and replace to first occurrence of string

Hi all,

I have a very large; delimited file. In vi I would like to replace:
CSACT_DY;AVG_UEACT1;uesPerActiveLinkSetSize_1;#;A
CSACT_DY;AVG_UEACT2;uesPerActiveLinkSetSize_2;#;A
CSACT_DY;AVG_UEACT3;uesPerActiveLinkSetSize_3;#;A

with:
CSACT_DY;AVG_UEACT1;Average uesPerActiveLinkSetSize_1;#;A
CSACT_DY;AVG_UEACT2;Average uesPerActiveLinkSetSize_2;#;A
CSACT_DY;AVG_UEACT3;Average uesPerActiveLinkSetSize_3;#;A

The only common thing is the AVG_. The last part is various lengths and ends in a mixture of numbers and capital letters

I tried
:%s/AVG_\(.*\);/AVG_\1;Average
but this matches to the last ;
Is there any way to change the default in vi to match to the first ;?

I have managed to hack around it by:
cat file.txt| awk -F\; '{print $1 ";" $2 "####" $3 ";" $4 ";" $5}' > file_2.txt
vi file_2.txt
:%s/AVG_\(.*\)####/AVG_\1;Average /
:%s/####/;

All weird and wonderful solutions welcome. Knowing vi, there must be about a hundred ways of doing this.

Cheers!
Reply With Quote
Forum Sponsor
  #2  
Old 05-02-2006
Registered User
 

Join Date: Aug 2001
Posts: 179
sed -e "s/;u/;Average u/" filename

or
%s/;u/;Average u
in vi

Last edited by sssow; 05-02-2006 at 01:14 PM.
Reply With Quote
  #3  
Old 05-02-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
Code:
sed 's/;/;Average /2' myFile.txt
Reply With Quote
  #4  
Old 05-03-2006
Registered User
 

Join Date: May 2006
Posts: 5
Sorry, I didn't make myself clear in my original posting. The snippet of the file was tiny and the first letter isn't always 'u'. Also, I only want to add in the word average when the previous word starts with AVG_.
So as an small example the text can contain things like:

CL_SUPD;URAUPDOC;uraUpdate_Other causes;#;A
CL_SUPD;URAUPPUU;uraUpdate_3_0_1 periodic URA update;#;A
CSACT_DY;AVG_UEACT1;uesPerActiveLinkSetSize_1;#;A
CSACT_DY;AVG_UEACT2;ActiveLinkSetSize;#;A
CSACT_DY;AVG_UEACT3;uesPerActiveLinkSetSize_2;#;A
CSACT_DY;BH_ACRALLCF;acRejections all causes;#;A
CSACT_DY;BH_ACRCDALF;acRejections_1_9_8 Code allocation failure;#;A

I would like to change only the AVG_ lines to:

CL_SUPD;URAUPDOC;uraUpdate_Other causes;#;A
CL_SUPD;URAUPPUU;uraUpdate_3_0_1 periodic URA update;#;A
CSACT_DY;AVG_UEACT1;Average uesPerActiveLinkSetSize_1;#;A
CSACT_DY;AVG_UEACT2;Average ActiveLinkSetSize;#;A
CSACT_DY;AVG_UEACT3;Average uesPerActiveLinkSetSize_2;#;A
CSACT_DY;BH_ACRALLCF;acRejections all causes;#;A
CSACT_DY;BH_ACRCDALF;acRejections_1_9_8 Code allocation failure;#;A
Reply With Quote
  #5  
Old 05-03-2006
Registered User
 

Join Date: May 2006
Posts: 5
Done it! The part after AVG_ is always * characters or less, so this works:

:%s/AVG_\([A-Z0-9]\{1,8\};\)/AVG_\1Average /
Reply With Quote
  #6  
Old 05-03-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Code:
sed -e "s/\(.*AVG_[^;]*;\)\(.*\)/\1 Average\2/g"
Reply With Quote
  #7  
Old 05-03-2006
Registered User
 

Join Date: May 2006
Posts: 5
Quote:
Originally Posted by vino
Code:
sed -e "s/\(.*AVG_[^;]*;\)\(.*\)/\1 Average\2/g"
Close, but no cigar
That worked for everything except where the AVG_**** was repeated in the third column. Eg:
RSUSG_DY;AVG_AVGDHT;AVG_AVGDHT;#;A
became:
RSUSG_DY;AVG_AVGDHT;AVG_AVGDHT;Average #;A

This approach is much better than mine above as it does not depend on the number of characters before the ;, so I had a fiddle with it and the following works:
sed -e "s/AVG_\([^;]*;\)\(.*\)/AVG_\1Average \2/"
Also taking out a bit of complexity, the following works:
sed -e "s/AVG_\([^;]*;\))/AVG_\1Average /"
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 03:50 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0