09-08-2023, 12:00 PM
How to Print Only the Replaced Lines With the sed Command
sed displays the entire file content and its substitute text in the output by default. Add the necessary attributes to the command if you have a lot of text and want to highlight the modified lines.
The -n option disables automatic printing, while the p command instructs sed to display strings where substitution occurs.
Here’s the general syntax:
sed -n 's/old_string/new_string/p' samplefile.txt
For example, to replace the third instance of “green” with “blue” in a line inside the colors.txt file and print the modified text on the terminal window, enter:
sed -n 's/green/blue/3p' colors.txt

