How do I perform sed substitution on a range of lines?

I want sed to read a text file, find a specific series of numbers, and replace them with another series of numbers.

However, I only want this to be done for a given range, for example lines 200-220. Here I can find pages on how to do this or that, rather than both.

My attempts so far are as follows:

sed -i '200,220 !/1 2 3 4 5 \replacement numbers' file

      

I'm probably approaching this completely wrong, but hey ho.

+3


source to share


1 answer


You were close

sed -i '200,220 s/1 2 3 4 5 /replacement numbers/' file

      



in the range 200-220 this will replace "1 2 3 4 5" with other things only once (per line). If more replacements are needed than the appended g here ... numbers / g 'file etc.

+4


source







All Articles