Athanasios Kataras
Athanasios Kataras

Reputation: 26440

sed: Search a file and replace a pattern in line

I want to replace a pattern in some lines:

I have tried

sed -i 's/pattern/new-string/g' ./file 

But this replaces everything in the file. I want to replace the pattern only on those lines that match another-pattern

Any thoughts?

Upvotes: 1

Views: 96

Answers (1)

anubhava
anubhava

Reputation: 785761

You can use this sed command:

sed -i '/another-pattern/s/pattern/new-string/g' ./file 

This will perform substitution only on lines that match another-pattern pattern.

Upvotes: 4

Related Questions