Search and replace to first occurrence of string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search and replace to first occurrence of string
# 1  
Old 05-02-2006
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  
Old 05-02-2006
sed -e "s/;u/;Average u/" filename

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

Last edited by sssow; 05-02-2006 at 05:14 PM..
# 3  
Old 05-02-2006
Code:
sed 's/;/;Average /2' myFile.txt

# 4  
Old 05-03-2006
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  
Old 05-03-2006
Done it! The part after AVG_ is always * characters or less, so this works:

:%s/AVG_\([A-Z0-9]\{1,8\};\)/AVG_\1Average /
# 6  
Old 05-03-2006
Code:
sed -e "s/\(.*AVG_[^;]*;\)\(.*\)/\1 Average\2/g"

# 7  
Old 05-03-2006
Quote:
Originally Posted by vino
Code:
sed -e "s/\(.*AVG_[^;]*;\)\(.*\)/\1 Average\2/g"

Close, but no cigarSmilie
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 /"
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. Shell Programming and Scripting

Replace first occurrence of a string in while loop

####Solved#### Hello, My aim is to replace searched string with incremented value under ubuntu 16.04. Example: aasasasas 9030 31wwo weopwoep weerasas 9030 ew31wo ieopwoep bbqqqsas 9030 ew3swo ieeopwoep ccsaqpas 9030 ewiro o2opwoep Expected: aasasasas 9030 31wwo weopwoep weerasas 9031... (2 Replies)
Discussion started by: baris35
2 Replies

3. AIX

Replace consecutive occurrence of string in same line

Hi All, I have a requirement to replace consecutive occurence of same string nedd to be replaced. Below is the input and desired output. Input: --------- 123.5|ABC|.|.|. 234.4|DEF|.|.|.|.|.| Output: --------- 123.5|ABC|||. 234.4|DEF||||| so basically "|.|" need to be replaced with... (9 Replies)
Discussion started by: ureddy
9 Replies

4. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

5. Shell Programming and Scripting

Replace x Number of String Occurrence with Sed

Ok, So I have a huge file that has over 12000 lines in it. in this file, there are 589 occurrences of the string "use five-minute-interval" spread in various areas in the file. How can i replace the the last 250 of the occurrences of "use five-minute-interval" with "use... (10 Replies)
Discussion started by: SkySmart
10 Replies

6. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

7. Shell Programming and Scripting

Search, replace string in file1 with string from (lookup table) file2?

Hello: I have another question. Please consider the following two sample, tab-delimited files: File_1: Abf1 YKL112w Abf1 YAL054c Abf1 YGL234w Ace2 YKL150w Ace2 YNL328c Cup9 YDR441c Cup9 YDR442w Cup9 YEL040w ... File 2: ... ABF1 YKL112W ACE2 YLR131C (9 Replies)
Discussion started by: gstuart
9 Replies

8. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

9. Shell Programming and Scripting

SED replace string by occurrence

hi all, I have a text file with following content PAGENUMBER asasasa asasasa PAGENUMBER sasasasasa PAGENUMBER using sed i want to replace PAGENUMBER by occurrence count eg 1 asasasa asasasa 2 sasasasasa 3 (4 Replies)
Discussion started by: uttamhoode
4 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question