Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Print Only the Replaced Lines With the sed Command
#1

    How to Print Only the Replaced Lines With the sed Command
   

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
   

   
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)