Facebook Twitter Instagram
    WiredRevolution.com
    • Home
    • About
    • Contact Us
    • Essential Linux Commands
    • Sitemap
    Facebook Twitter Instagram
    WiredRevolution.com
    bash

    Delete a specific line from a text file with sed

    RyanBy RyanOctober 26, 2008Updated:November 3, 20086 Comments1 Min Read
    Facebook Twitter Pinterest LinkedIn Reddit WhatsApp Email

    At some point you may have the need to remove all lines within a text file that match a certain pattern. Accomplishing this is easy with the sed command.

    Here is the command format.

    $ sed -i '/PATTERN/ d' file.txt
    
    • The ‘-i‘ option allows you to edit the specified file in place.
    • PATTERN is a regular expression
    • d is a command which instructs sed to delete the line if it matches the PATTERN

    Here is an example.

    $ cat file.txt
    
    one
    two
    three
    one hundred
    

    We want to remove the lines containing the word “one”

    $ sed -i '/one/ d' file.txt
    
    $ cat file.txt
    
    two
    three
    

    If your pattern contains an environment variable, you can surround it with double quotes instead to force the shell to expand it.

    $ sed -i "/$MYVAR/ d" file.txt
    
    Share. Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Email
    Previous ArticleList all open files with lsof
    Next Article Disable ext3 boot-time check with tune2fs

    Related Posts

    Safely and atomically change a symlink target

    Enable the backspace key in VIM

    Have a Bash script determine it’s own location

    Most Commented
    March 12, 2009

    Fix blue tinted video in Ubuntu

    September 10, 2010

    Setup SSH access between VirtualBox Host and Guest VMs

    March 8, 2011

    Install GNOME Shell in Ubuntu 10.10 Maverick

    April 4, 2009

    Setup the PS3 Bluetooth Controller on Ubuntu

    October 22, 2008

    How to correctly use LD_LIBRARY_PATH

    Recent Comments
    • Execute command on linux virtual machine (or server) from windows commandline on Setup SSH access between VirtualBox Host and Guest VMs
    • Solved: How to SSH to a VirtualBox guest externally through a host? - Daily Developer Blog on Setup SSH access between VirtualBox Host and Guest VMs
    • How to SSH to a VirtualBox guest externally through a host? [closed] – Code D3 on Setup SSH access between VirtualBox Host and Guest VMs
    • How to copy and paste from VirtualBox? [duplicate] on Setup SSH access between VirtualBox Host and Guest VMs
    • Jackie Laguna on Fix OpenGL: ChoosePixelFormat SketchUp error in WINE
    © 2025 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.