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
Getting the lines between last occurrence of two patterns rvz Shell Programming and Scripting 8 08-08-2008 06:32 PM
SED replace string by occurrence uttamhoode Shell Programming and Scripting 4 03-05-2008 05:04 AM
awk + last occurrence agibbs UNIX for Dummies Questions & Answers 2 10-06-2007 04:32 PM
Search and replace to first occurrence of string gilmord UNIX for Dummies Questions & Answers 7 05-03-2006 08:43 AM
Grep for the same occurrence or maybe Sed hcclnoodles Shell Programming and Scripting 4 09-12-2002 01:25 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 Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 12-26-2007
lyoncc lyoncc is offline
Registered User
  
 

Join Date: May 2007
Posts: 22
Replace second occurrence only

HPUX /bin/sh (posix)

I have a file as such

cat dog mouse
deer elk rabbit
mouse rat pig

I would like to replace the second occurrence of mouse in this file with mouse2. The rest of the file has to stay exactly as is. I'm not sure exactly where mouse might be (could be first,second,third field).

TIA!!!!!!
  #2 (permalink)  
Old 12-26-2007
frank_rizzo frank_rizzo is offline Forum Advisor  
Resident BOFH
  
 

Join Date: Dec 2007
Posts: 426
Put this code in mouse.awk


Code:
/mouse/{
   mcount++
   if(mcount == 2){
       sub("mouse","mouse2",$0)
   }
}

{print}

awk -f mouse.awk animals.txt
  #3 (permalink)  
Old 12-26-2007
rikxik's Avatar
rikxik rikxik is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 250
So what happens if the file has this:

cat mouse mouse
  #4 (permalink)  
Old 12-26-2007
frank_rizzo frank_rizzo is offline Forum Advisor  
Resident BOFH
  
 

Join Date: Dec 2007
Posts: 426
Quote:
Originally Posted by rikxik View Post
So what happens if the file has this:

cat mouse mouse
the poster said they wanted the second occurrence. this does that. if you need all occurrences of mouse changed on the line where the second occurrence is found then change sub to gsub.
  #5 (permalink)  
Old 12-26-2007
rikxik's Avatar
rikxik rikxik is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 250
I think I was not clear. I meant, for "cat mouse mouse", the second "mouse" on this line also counts as a second ocurrance. So shouldn't that change to "cat mouse mouse2"?
  #6 (permalink)  
Old 12-26-2007
DeepakS's Avatar
DeepakS DeepakS is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 74
This'll take care of it:


Code:
awk -f mouse.awk RS=" " ORS=" " animals.txt


Code:
$ cat animals.txt 
cat dog pig 
deer elk rabbit
mouse mouse mouse

$ awk -f mouse.awk RS=" " ORS=" " animals.txt 
cat dog pig 
deer elk rabbit
mouse mouse2 mouse

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 03:14 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