How to use sed to update the first occurrence of a text after a section specified in a file

advertisements

I have a file with the content:

#Section1
port=3000
port=3000

#Section2
port=3000
port=3000

I want to update the first occurrence of "port=3000" to "port=1000" after the text "#Section2". Here is the output:

#Section1
port=3000
port=3000

#Section2
port=1000
port=3000

Can anyone give me some hints about how to use sed for it?

Thanks


This might work for you (GNU sed):

sed '/#Section2/,/port/{s/\(port=\).*/\11000/}' file