09-08-2023, 11:57 AM
Replace String on a Range of Lines Using the sed Command
Add the line numbers to the sed command to replace several strings within a specific range. Replace the first # with the initial text number and the second # with the last row you want to include.
Here’s the basic syntax:
sed '#,# s/old_string/new_string/' samplefile.txt
For example, the command below replaces the last three instances of “germany” located in the third, fourth, and fifth line on the countries.txt file, with the word “france”:
sed ‘3,5 s/germany/france/’ countries.txt

