![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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! |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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. |
|
#3
|
||||
|
||||
|
Code:
sed 's/;/;Average /2' myFile.txt |
|
#4
|
|||
|
|||
|
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
|
|||
|
|||
|
Done it! The part after AVG_ is always * characters or less, so this works:
:%s/AVG_\([A-Z0-9]\{1,8\};\)/AVG_\1Average / |
|
#6
|
||||
|
||||
|
Code:
sed -e "s/\(.*AVG_[^;]*;\)\(.*\)/\1 Average\2/g" |
|
#7
|
|||
|
|||
|
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 /" |
|||
| Google The UNIX and Linux Forums |