0

I need to change text in a file before and after non-fixed strings.
I'll show an example:

this-is-cat-really-weird  
this-is-dog-really-weird  

there-is-cat-really-weird  
there-is-dog-really-weird  

that-is-cat-really-weird  
that-is-dog-really-weird  

I need to write a single instruction to change only the first two lines, as the desired output will be the following:

this-is-cat-really-nice  
this-is-dog-really-nice  

there-is-cat-really-weird  
there-is-dog-really-weird  

that-is-cat-really-weird  
that-is-dog-really-weird 

I've not beeb able to found a suitable solution using awk.
Also, how can I write a working instruction if I have 100+ lines like these that I'd like to change, and these lines could be anywhere in the file?
Any help will be appreciated.

  • I've deleted my answer because I don't understand the - updated - question – steeldriver Aug 21 '18 at 0:20
1

Use sed with range of lines:

$ sed '1,2s/weird/nice/' input.txt
this-is-cat-really-nice  
this-is-dog-really-nice  

there-is-cat-really-weird  
there-is-dog-really-weird  

that-is-cat-really-weird  
that-is-dog-really-weird  

To match anywhere in the file:

$ sed '/this-is-cat-really/{s/weird/nice/;N;s/weird/nice/}' input.txt 
this-is-cat-really-nice  
this-is-dog-really-nice  

there-is-cat-really-weird  
there-is-dog-really-weird  

that-is-cat-really-weird  
that-is-dog-really-weird  

this-is-cat-really-nice  
this-is-dog-really-nice  
  • Thanks for your answer. Please check the update in the end of the question (I forgot to write it earlier). – Rodrigo Andres Nava Lara Aug 20 '18 at 23:54
  • 1
    @RodrigoAndresNavaLara After the edit, your question asks two different things. I've edited the answer, but for future please remember this is not proper - only one question per post; otherwise this makes the post unclear and it will be closed – Sergiy Kolodyazhnyy Aug 21 '18 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.