Sed: Find and Replace The Whole Line [ Regex ]

ww‮al.w‬utturi.com
Sed: Find and Replace The Whole Line [ Regex ]

To find and replace the entire line using the sed command, you can use the ^ and $ regular expression characters to match the start and end of the line, respectively.

For example, to replace all lines in a file that contain the word "apple" with the word "orange", you can use the following sed command:

sed 's/^.*apple.*$/orange/' input.txt > output.txt

This will replace all lines in the input.txt file that contain the word "apple" with the word "orange", and save the modified output to the output.txt file. The ^ and $ characters anchor the regular expression to the start and end of the line, respectively, so that only lines that contain the word "apple" are replaced.

You can also use this approach to replace lines that match a specific pattern. For example, to replace all lines that start with the word "apple" with the word "orange", you can use the following sed command:

sed 's/^apple.*$/orange/' input.txt > output.txt

This will replace all lines in the input.txt file that start with the word "apple" with the word "orange", and save the modified output to the output.txt file.

Created Time:2017-10-30 14:27:18  Author:lautturi