replace nth instance of string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace nth instance of string
# 1  
Old 04-02-2008
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
# 2  
Old 04-02-2008
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.
# 3  
Old 04-02-2008
Code:
#   cat infile | nawk '/TEST/{n+=1}{if (n==3){sub("TEST","OK",$0)};print }'
...........................
..........TEST..........
..........TEST..........
.....................
.....OK..........
.....................
.....................
.....TEST..........

HTH
# 4  
Old 04-03-2008
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
# 5  
Old 04-03-2008
Code:
nawk -v p=$1 'BEGIN{n=1}
$0 !~ /TEST/{print}
$0 ~ /TEST/ {
if (n!=p)
{
	n=n+1
	print $0
}
else
	
{
	sub(/TEST/,"OK",$0)
	print $0
	n=n+1
}
}' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace every second instance of delimeter

Hi, Need help on replacing every second instance of delimeter. Scenario: var="Name1,Value1,Name2,Value2,Name3,Value3,Name4,Value" I want every second "," to replace with "|" I tried like below echo $var| sed 's/,/|/2' But, it's not working. Expected output: ... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. Shell Programming and Scripting

How to search and replace string from nth column from a file?

I wanted to search for a string and replace it with other string from nth column of a file which is comma seperated which I am able to do with below # For Comma seperated file without quotes awk 'BEGIN{OFS=FS=","}$"'"$ColumnNo"'"=="'"$PPK"'"{$"'"$ColumnNo"'"="'"$NPK"'"}{print}' ${FileName} ... (5 Replies)
Discussion started by: Amit Joshi
5 Replies

3. Shell Programming and Scripting

Replace nth to nth character?

Hi I got the following problem and I wonder if some could please help me out? I'd like to replace character 8 - 16 , 16 - 24 cat file ... (2 Replies)
Discussion started by: stinkefisch
2 Replies

4. Shell Programming and Scripting

sed replace nth characters with string

Hi, I hope you can help me out please? I need to replace from character 8-16 with AAAAAAAA and the rest should stay the same after character 16 gtwrhtrd11111111rjytwyejtyjejetjyetgeaEHT wrehrhw22222222hytekutkyukrylryilruilrGEQTH hrwjyety33333333gtrhwrjrgkreglqeriugn;RUGNEURGU ... (4 Replies)
Discussion started by: stinkefisch
4 Replies

5. Shell Programming and Scripting

Replace a value of Nth field of nth row

Using Awk, how can I achieve the following? I have set of record numbers, for which, I have to replace the nth field with some values, say spaces. Eg: Set of Records : 4,9,10,55,89,etc I have to change the 8th field of all the above set of records to spaces (10 spaces). Its a delimited... (1 Reply)
Discussion started by: deepakwins
1 Replies

6. Shell Programming and Scripting

Complex find and replace only 1st instance string with dynamic combination

test.txt is the dynamic file but some of combination are fix like below are the lines ;wonder_off = ;wonder_off = disabled wonder_off = wonder_off = disabled the test.txt can content them in any order #cat test.xt ;wonder_off = ;wonder_off = disabled wonder_off = wonder_off =... (5 Replies)
Discussion started by: SilvesterJ
5 Replies

7. Shell Programming and Scripting

Replace all but skip first instance in a line

I have a record like the one given below. 010000306551~IN ~N~ |WINDWARD PK|Alpharetta| If ~ is present more than instance in a line,then I need to delete those instances. Any ideas? I am working in Solaris (7 Replies)
Discussion started by: prasperl
7 Replies

8. Shell Programming and Scripting

extract data between nth and n+1th instance of a line

folks.. i need a simple one liner to extract data from between the (n)th and (n+1)th instance of a line in a 2 colum file. eg....for n=3 i should get back 0 1 4 6 help would be much appreciated. file blah.txt ################## identifer line 0 3 0 3 identifer line 0 2 0... (5 Replies)
Discussion started by: ryanp200
5 Replies

9. Shell Programming and Scripting

sed replace 2nd instance

Hello, I want to replace 2nd instance of "foo" in a file use sed. Any suggestions? (2 Replies)
Discussion started by: katrvu
2 Replies

10. Shell Programming and Scripting

replace first instance(not first instance in line)

Alright, I think I know what I am doing with sed(which probably means I don't). But I cant figure out how to replace just the first occurance of a string. I have tried sed, ed, and grep but can't seem to figure it out. If you have any suggestions I am open to anything! (3 Replies)
Discussion started by: IronHorse7
3 Replies
Login or Register to Ask a Question