sed -d

Example

#pool 0.ubuntu.pool.ntp.org iburst #pool 1.ubuntu.pool.ntp.org iburst #pool 2.ubuntu.pool.ntp.org iburst #pool 3.ubuntu.pool.ntp.org iburst pool ntp.ubuntu.com

Deleting lines of the form #pool 0.ubuntu.… can be done with the command

sed '/^#pool\s[0-9]\.ubuntu/ d' /etc/ntp.conf

I remind you that \s means one space

[0-9] means one arbitrary digit

Delete a line by its number

test.txt

sed -i 3d test.txt

Delete a line in which there is no certain pattern

For such operations you can use !d - this is the opposite of the d operation - lines where the pattern is absent are deleted, that is, the ones where the pattern is present remain.

sed '/pattern/!d' file

You can use -n

sed -n '/pattern/p' file

For example, in the file visited.log There are lines of the form

… https://www.heihei.ru/Finland/life/integration_course.php https://www.topbicycle.ru/m/usa/ https://www.topbicycle.ru/b/ https://www.heihei.ru/Finland/travel/ https://www.heihei.ru/Spain/cities/benalmadena/paloma.php https://eth1.ru/testing/edu/ …

If the task is to write out only the visits to a separate file heihei.ru

sed '/heihei/!d' visited.log > hh.log
cat hh.log

…
https://www.heihei.ru/Finland/life/integration_course.php
https://www.heihei.ru/Finland/travel/
https://www.heihei.ru/Spain/cities/benalmadena/paloma.php
…

Delete the first few lines

Suppose you want to delete the first three lines of the file example.txt

sed -i -e 1,3d example.txt

Delete empty lines

If the line is really empty, the command will do

sed '/^$/d'

Usually life is harsher, and the lines contain spaces.

Such lines can be deleted too

$ sed '/^[[:space:]]*$/d' input.txt > output.txt

Delete comments

Suppose you have code or just text with many comments.

A comment line starts with the # character

Consider the file websites

cat websites

# Travel https://www.heihei.ru # Bicycles https://www.topbicycle.ru # IT https://www.eth1.ru

To delete lines with comments, run

sed -i '/^#/d' websites

The -i option allows you to modify the current file

If you want to keep the source file and write the text without comments into a new one (if you have already deleted the comments, make sure you have restored them)

sed '/^#/d' websites > nocomments
cat nocomments

https://www.heihei.ru https://www.topbicycle.ru https://www.eth1.ru

The -i option is not needed since we did not modify the source file

To delete comment lines and empty lines, run

sed -i '/^#/d ; /^$/d' websites

Delete comments and empty lines

Using the example nginx.conf

vi nginx.conf

user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off;

sed -i.bak '/^\s*#/d;/^$/d' nginx.conf
vi nginx.conf

user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; …

Delete a line with a word

Delete all lines where the word Apple occurs in the file input.txt

Here is an Apple Here Pen Here ApplePen Integer is Here Here is a Float Here Pen Here Pineapple Here PineapplePen Umm Apple Apple Apple Pen

This can be done with the option d

sed '/Apple/d' input.txt > output.txt

Result:

Integer is Here Here is a Float Here Pen Here Pineapple Here PineapplePen

ApplePen

Let us complicate the condition - delete all lines that contain the word Pineapple and the word Integer

sed '/Pineapple\|Integer/d' input.txt > output.txt

| acts as a logical OR

\ is needed to escape |

Result:

Here is an Apple. Here is a Pen. Here is an ApplePen Here is a Float Umm Apple Apple Apple Pen

Deletion without d

Files can be deleted from without using d too

Other deletion options
Removing newline characters
Truncate a file - delete everything starting from a certain line
Delete everything on the left
Delete everything on the right
Delete everything between brackets

Article author: Andrei Olegovich