Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 10-08-2008, 03:42 AM   #1
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


num_oper = rim1
oper = rim
Old 10-08-2008, 03: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


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 04:50 AM
SED replace string by occurrence uttam_h Programming 5 03-05-2008 10:02 PM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 03:07 AM
Replace every other occurrence of pattern Wynd Linux - General 8 12-14-2005 03:43 PM

All times are GMT -5. The time now is 02:55 PM.

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