Linux / Unix: Sed Delete Matching Words In A File

last updated in Categories , ,

I have a file called rsnapshot.conf. It has the words “ssh_args=-p 1222”. How do I match and remove (delete) the words “ssh_args=-p 1222” from config file using sed command under Linux or Unix like operating systems?

You can use the the substitute sed command changes all occurrences of the “ssh_args=-p 1222”. The same command can be used to delete the required words.

Sed command syntax to remove words

The syntax is:

sed 's/word-to-find//g' input.file > output.file

Gnu/sed syntax:
sed -i 's/word-to-find//g' input.file

Examples

Type the following command

sed  -i 's/ssh_args=-p 1222//g' input.file
sed  -i 's/ssh_args=-p 1222//g' /etc/rsnapshot.conf

Get 15% off on Linux Foundation certified SysAdmin, Progamming, Kubernetes/Containers and Open Stack certification & course. Use "SPLASH15" coupon code. Offer expires on August 27, 2018
training.linuxfoundation.org

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

1 comment

  1. what is the command to find a word in a line, then deleting (that line and previous line)

    example, I want to search for the word JDK, delete the JDK line and the previous line.

    INPUT
    ————————-
    Version 5.1
    Version 5.1.1.12
    ID BASE
    Version 1.4.2
    ID JDK
    Version 5.1
    Version 1.4.2
    ID JDK
    Version 5.1.1.12
    ID ND

    OUTPUT
    —————————
    Version 5.1
    Version 5.1.1.12
    ID BASE
    Version 5.1
    Version 5.1.1.12
    ID ND

    Have a question? Post it on our forum!