How to use sed command

Published on: 2023-10-25

How write changes from sed to file

READ THIS CHAPTER FIRST

If you want to apply deletion changes to file you have to use option -i or redirect output with shell syntax >.

If you do not add one of above options, then the result will be printed to stdin instead to a file resp. a new file.

If you are not sure what the output will be, then do not use option -i it can removed yo u whole file. Instead use > or without any option to print just to stdin to test your intentions.

The option -i will overwrite existing file which has been used as a source. The option means in place. If you’re using BSD (macOS included) sed, then you have to use this syntax:

$ sed -i ''

or

$ sed -i ""

If you are using GNU sed then syntax below is enough

$ sed -i

Redirecting output with > will create you wholly new file and the source file will be un touched.

$ sed [sed_pattern] [src_filepath] > [new_filepath]

All examples below are write for macOS.

Delete all empty lines

$ sed -i "" '/^$/d' file.txt

^ - start of line $ - end of line

Delete lines which contains only START and END.

Delete one line specified by line number

$ sed -i "" '[line_number]d' [file_path]
$ sed -i "" '7d' file.txt

Or you can delete several lines one by one

$ sed -i "" '[line_number]d;[line_number]d' [file_path]
$ sed -i "" '7d;12d;16d' file.txt

Delete last line

$ is symbol wich represent last line.

$ sed '$d' input.txt

Delete range of lines specified by lines number

$ sed -i "" '[start_line_number],[end_line_number]d' [file_path]

Delete lines from line number 3 to line number 9.

$ sed -i "" '3,9d' file.txt

Delete several ranges of lines

$ sed -i "" '[first_range]; [nth_range]d' [file_path]

Delete lines specified in two ranges. Delete lines 1 to 3(included) and lines from 7 to 9.

$ sed -i "" '1,3d; 7,9d' input.txt

Delete from 1 to 3 and from 7 to the end of file.

$ sed -i "" '1,3d; 7,$d' input.txt

Delete all lines which match pattern

$ sed -i "" '/[pattern]/d' [file_path]

Delete all lines which contains “hello"

$ sed -i "" '/hello/d' file.txt

If you want to ingorecase of pattern

$ sed -i "" '/hello/I d' file.txt

Delete all lines which not matching pattern

$ sed -i "" '/[pattern]/!d' [file_path]

Delete all lines which not contains “hello"

$ sed -i "" '/hello/!d' file.txt

Delete range of lines specified by pattern

Delete all lines between lines which START line contains start_pattern and END line contains end_pattern. Lines with patterns are included in deletion.

$ sed -i "" '/[start_pattern]/,/[end_pattern]/d' [file_path]

Delete all lines between lines where start line contains ‘Hello’ and end line contains ‘Thanks’, including start and end lines.

$ sed -i "" '/Hello/, /Thanks/d' file.txt