LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to find and replace only the 2nd occurrence of similar string in a file (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-find-and-replace-only-the-2nd-occurrence-of-similar-string-in-a-file-674990/)

hchoonbeng 10-08-2008 03:42 AM

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

Mr. C. 10-08-2008 03:44 AM

In this case, use

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


All times are GMT -5. The time now is 06:20 PM.