≡ Menu

Unix Sed Tutorial: Delete File Lines Using Address and Patterns

Linux Sed Command Tutorial with Awesome ExamplesIn the previous sed tutorial we discussed about Unix sed command basics and printing lines from a file using sed address and patterns.

In this article, let us review how to delete lines from a file using address and patterns with 8 awesome examples.

  • “p” command prints the buffer (remember to use -n option with “p”)
  • “d” command is just opposite, its for deletion. ‘d’ will delete the pattern space buffer and immediately starts the next cycle.
Syntax:

# sed 'ADDRESS'd filename

# sed /PATTERN/d filename

Syntax for ADDRESSES and PATTERNS given in the printing is applicable for deletion also, except -n option. (-n only to suppress printing pattern buffer, can be used with “p” command )

Let us first creates thegeekstuff.txt file that will be used in all the examples mentioned below.

# cat thegeekstuff.txt
1. Linux - Sysadmin, Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

1. Delete Nth Line

‘Nd’ deletes the Nth line and prints the other lines.

sed ‘Nd’ filename

As per sed methodology,

  • It reads the first line and places in its pattern buffer.
  • Check whether supplied command is true for this line, if true, deletes pattern space buffer and starts next cycle. i.e Read next line.
  • If supplied command doesnt true, as its normal behaviour it prints the content of the pattern space buffer.

For example, 3d deletes 3rd line and prints other lines as shown below.

$ sed 3d thegeekstuff.txt
1. Linux - Sysadmin, Scripting etc.
2. Databases - Oracle, mySQL etc.
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

2. Delete Starting from 3rd line and every 2nd line from there.

$ sed '3~2d' thegeekstuff.txt
1. Linux - Sysadmin, Scripting etc.
2. Databases - Oracle, mySQL etc.
4. Security (Firewall, Network, Online Security etc)
6. Cool gadgets and websites
8. Website Design
10.Windows- Sysadmin, reboot etc.

3. Delete from 4th to 8th line from file.

$ sed '4,8d' thegeekstuff.txt
1. Linux - Sysadmin, Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Hardware
9. Software Development
10.Windows- Sysadmin, reboot etc.

4. Delete the last line from input.

$ sed '$d' thegeekstuff.txt
1. Linux - Sysadmin, Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development

5. Delete the line which matches the given pattern from input.

For example, the below command deletes the line which matches with “Sysadmin”.

$ sed /Sysadmin/d thegeekstuff.txt
2. Databases - Oracle, mySQL etc.
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development

6. Deletes the line from which matches the given pattern to end of the file.

$ sed '/Website/,$d' thegeekstuff.txt
1. Linux - Sysadmin, Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)

7. Deletes the line from which matches the given pattern and 2lines next to that.

$ sed '/Storage/,+2d' thegeekstuff.txt
1. Linux - Sysadmin, Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Hardware
4. Security (Firewall, Network, Online Security etc)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

8. Delete blank Line from a file using sed

You can also remove blank lines with sed. The following sed example shows how to use sed and remove blank lines.

$ sed '/^$/d' thegeekstuff.txt
1. Linux - Sysadmin, Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 21 comments… add one }

  • Selva November 21, 2009, 12:09 am

    How to match “file path” and delete the matched line

  • ashwini June 18, 2010, 11:45 am

    The information about sed is very nice.

  • JAYAPRAKASH June 30, 2010, 9:43 pm

    Nice tutorial…………Thanks

  • shivakumar March 16, 2011, 4:38 am

    Nice notes ..very helpful ..

  • priyesh July 29, 2011, 1:07 am

    excellent work very useful
    thanks !

  • venkat October 13, 2011, 2:37 am

    very useful material..
    thank you verymuch.

    Venkat V

  • bromario October 18, 2011, 1:38 pm

    How to delete last 5 lines of file

  • sid November 20, 2011, 5:52 pm

    Hi, How do i delete lines from second occurance of a string to end of file?

  • john November 21, 2011, 11:46 am

    how can i delete multiple lines from a file ?

    GNU sed version 4.2.1
    sed ‘/word1/,/word2/d’ file1 > file2 works ok

    but when i try more words…
    sed ‘/word1/,/word2/,/word3/,/word4/d’ file1 > file2
    sed: -e expression #1, char 16: unknown command: `,’

    my question is how can i delete more words from a file .,. ?
    Thanks

  • shashank November 22, 2011, 6:36 am

    Very well explained…

  • sidhant March 27, 2012, 11:46 pm

    how to delete multiple comment in unix
    eg:

    /*********************************************
    *hello
    **********************************************/

  • Saurabh September 22, 2012, 2:54 pm

    Hi,

    I am trying to delete 18th line from file.
    sed 18d /etc/passwd

    After this command its hows 1-17th line and it looks 18th line is deleted. However when i am again checking cat /etc/passwd. It is showing me the 18th line ?

    Can someone out some light on this or i am missing something?

  • sara November 3, 2012, 4:44 am

    @Saurabh :

    Command is correct but , While using sed, output cannot be written into same file unless you use sed -i (new from Sed version 4.1.4)

    Seems you are using older version.
    try something like this:

    sed ’18’d /etc/passwd > /tmp/passwd
    cp /tmp/passwd /etc/passwd

  • mstafreshi April 4, 2013, 1:41 pm

    sorry ! just for other visitors who has sidhant’s question :
    “how to delete multiple comment in unix?”
    —————————————————————-
    sed ‘/\/\*/ {
    # if you find /* branch to last label
    b last
    }

    # branch to END OF CYCLE
    b

    # find last lable
    :last
    /\*\// !{
    N
    b last
    }
    d

    ——————————————

  • saranya June 2, 2013, 12:35 am

    VERY USEFUL MATERIAL

  • Soumya January 25, 2014, 5:20 pm

    Hi Ramesh

    i have file in that i need to delete lines only if words consecutively matches in first column in matches with next line first column then delete both the lines , so file will have only non-repeated words.

    example file

    0x66cf 0xf0 17025 >>>
    0x66cf 0xf1 17185 >>> If you see column 1 line 1 and column 1 line 2 has same word which need to deleted
    0x66d0 0xf0 17028
    0x66d0 0xf1 17205
    0x66d1 0xf0 17032
    0x66d1 0xf1 17228
    0x66d2 0xf0 17036
    0x66d2 0xf1 17249
    0x66d3 0xf0 17039
    0x66d3 0xf1 17272
    0x66d4 0xf0 17043
    0x66d4 0xf1 17291
    0x66d5 0xf0 17188
    0x66d6 0xf0 17191
    0x66d7 0xf0 17194
    0x66d8 0xf0 17197
    0x66d9 0xf0 17200 >>>> only this type line should be available in file remove othe
    0x66da 0xf0 17204 >>>>
    0x66db 0xf0 17208 >>>>
    0x66dc 0xf0 17212 >>>>
    0x76d9 0xf1 17200 >>>>
    0x76da 0xf1 17204 >>>>
    0x76db 0xf1 17208 >>>>
    0x76dc 0xf1 17212 >>>>

    0x66dd 0xf0 17216
    0x66de 0xf0 17219

  • Washim Khan June 12, 2014, 1:19 am

    It is very help full tutorial for Beginners .

  • Bhanuprasad September 29, 2014, 3:50 pm

    How can I remove base64 malware string using awk instead of sed?

  • sasi October 26, 2014, 11:40 pm

    make it more explanative

  • sasi October 27, 2014, 7:46 pm

    Using sed, how to write a script that delete the line thats starts and ends with same pattern

  • Bhanuprasad November 13, 2014, 10:19 pm

    I mean, if the SED is blocked from /usr/bin/ for security purposes, then in that case how can we clean base64 malware strings ?

Leave a Comment