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 > 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 9 06-08-2009 06:11 AM
SED replace string by occurrence uttamhoode Shell Programming and Scripting 4 03-05-2008 05:04 AM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 10:24 AM
String Search & Replace IwishIknewC UNIX for Dummies Questions & Answers 1 03-25-2006 06:28 AM
string search replace krishna UNIX for Advanced & Expert Users 1 12-19-2001 01:49 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-02-2006
gilmord gilmord is offline
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!
  #2 (permalink)  
Old 05-02-2006
sssow sssow is offline
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 04:14 PM..
  #3 (permalink)  
Old 05-02-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
sed 's/;/;Average /2' myFile.txt
  #4 (permalink)  
Old 05-03-2006
gilmord gilmord is offline
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
  #5 (permalink)  
Old 05-03-2006
gilmord gilmord is offline
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 /
  #6 (permalink)  
Old 05-03-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Code:
sed -e "s/\(.*AVG_[^;]*;\)\(.*\)/\1 Average\2/g"
  #7 (permalink)  
Old 05-03-2006
gilmord gilmord is offline
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 /"
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 05:01 PM.


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