How do I use sed to substitute the third occurrence of the same regex in the file?

I have a problem finding a sed script that works when the regex looks exactly the same but I only want to substitute one.

The file looks like this:

something someting something
this 
something someting something
something someting something
this 
someting something
someting something
someting something
this
someting something
someting something
this

I want to substitute the third "this" to something else. I have tried with:

sed '3,/this.*/s/this.*/something/'

and various similar attempts, but it doesn't work.

Answers


This isn't easy in sed but awk can do it

awk '/this/{count++;if(count==3){sub("this","something")}}1'

Need Your Help

Getting blog text to wrap around menu DIV when menu rendered last

css wordpress templates css-float

I am working with WordPress and I am trying to modify my template so that on my blog pages. I have a menu on the right hand side of the page while the blog content is on the left. The problem I am ...

Create new jQuery ui tab and populate with Ajax data

jquery ajax tabs

I've got a set of jQuery ui tabs that starts with one tab which is pre-poluated with data from another page. That works fine. What I am trying to do is create a series of links that when clicked will

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.