By Keith Mitnick on Wednesday, 22 June 2016
Category: General

Using the sed command to find and replace and entire line of text in a document.

This command will replace the line that contains “This is a test” with “Is it Friday yet?” in the test.txt document.




sed -i "" '/This is a test/ c\

Is it Friday yet?\

' test.txt


Here is a breakdown of the above command.

The -i will edit the file in place.

The "" will get around not having an extension after the -i as required by os x.  

c\  is the change flag.  The above command is separated on multiple lines to make the c\ flag work properly.  If you try to enter the command on one line, it will fail because the c\ has issues with escaping the backslash.

You could also use sed -i.bak which would create a backup of the file before it’s edited instead of the "".

The remainder of the command:  replace the line that contains “This is a test” with “Is it Friday yet?” in the test.txt document.


Leave Comments