The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-02-2008
uttamhoode uttamhoode is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 18
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 (permalink)  
Old 04-02-2008
chella chella is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 75
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 (permalink)  
Old 04-02-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431

Code:
#   cat infile | nawk '/TEST/{n+=1}{if (n==3){sub("TEST","OK",$0)};print }'
...........................
..........TEST..........
..........TEST..........
.....................
.....OK..........
.....................
.....................
.....TEST..........

HTH
  #4 (permalink)  
Old 04-03-2008
uttamhoode uttamhoode is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 18
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 (permalink)  
Old 04-03-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,092

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

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0