LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 10-08-2008, 04:42 AM   #1
hchoonbeng
LQ Newbie
 
Registered: Sep 2008
Posts: 15

Rep: Reputation: 0
how to find and replace only the 2nd occurrence of similar string in a file


hi how do u replace only the 2nd occurence of similar string in a file?
eg in test.txt

num_oper = 1
oper = mem


i wrote a script to replace only oper the 2nd object in test.txt but it seem that both num_oper and oper got replaced. my script is as follow:

sed -n s/oper=[a-z]*/oper=rim/p test.txt

output:

num_oper = rim1
oper = rim
 
Old 10-08-2008, 04:44 AM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
In this case, use

sed -n s/^oper=[a-z]*/oper=rim/p
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and replace string Johng Programming 9 01-13-2010 05:50 AM
SED replace string by occurrence uttam_h Programming 5 03-05-2008 11:02 PM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 07:02 AM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 04:07 AM
Replace every other occurrence of pattern Wynd Linux - General 8 12-14-2005 04:43 PM


All times are GMT -5. The time now is 05:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration