![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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! |
|
||||
|
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 |
|
||||
|
Quote:
![]() 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 /" |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|