Deleting – the d command

The d command is used to delete lines. After sed copies a line from a file and puts it into a pattern buffer, it processes commands on that line, and, finally, displays the contents of the pattern buffer on screen. When the d command is issued, the line currently in the pattern buffer is removed and not displayed, as follows:

    $ cat country.txt
    Country     Capital     ISD Code
    USA         Washington  1
    China       Beijing     86
    Japan       Tokyo       81
    India       Delhi       91
    $ sed '3d' country.txt
  

The output is as follows:

    Country      Capital    ISD Code
    USA          Washington 1
    Japan        Tokyo           81
    India        Delhi       91
  

Here is the explanation.

The output will contain all the lines except the third line. The third line is deleted by the following command:

    $ sed '3,$d' country.txt ...

Get Learning Linux Shell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.