![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
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 07:11 AM |
| sed replace 2nd instance | katrvu | Shell Programming and Scripting | 2 | 02-12-2008 02:17 PM |
| replace first instance(not first instance in line) | IronHorse7 | Shell Programming and Scripting | 3 | 02-07-2008 01:29 PM |
| Replace string B depending on occurence of string A | hemangjani | Shell Programming and Scripting | 1 | 12-05-2006 05:10 PM |
| How to replace within a string | aukequist | Shell Programming and Scripting | 2 | 02-07-2006 07:42 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
replace nth instance of string
Hi all,
I have file with following content ........................... ..........TEST.......... ..........TEST.......... ..................... .....TEST.......... ..................... ..................... .....TEST.......... I want to replace nth "TEST" with "OK" using sed/awk/perl.... tried sed 's/TEST/OK/3' filename...but it will replace nth instance of each line.... Thanks and Regards, uttam hoode |
|
||||
|
Hi Uttam, I have some suggestions for your problem. 1. Make your file with a single line, such that all the 'TEST' pattern occurs in a single line. You can use tr command to do this. Code:
tr '\n' ';' < inp.txt > temp1.txt 2. Then use sed command to replace the nth occurrence of the 'TEST' pattern. Code:
sed 's/TEST/OK/5' temp1.txt > temp2.txt 3. Then split the single line using the same tr command. Code:
tr ';' '/n' < temp2.txt > out.txt It may be a very long and tedious process, but I think it will help you to proceed. Regards, Chella. |
|
||||
|
HI chella and Tytalus,
Thanx for the solution..... Tytalus can u please give me a sed quivalent for this command?....in sed i can use inline edit option (-i) and can modify the file without using cat,pipe or redirection... Regds, uttam |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|