Linux / Unix: Sed Delete Matching Words In A File

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.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements sed
Est. reading time N/A

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 the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • Amir Nov 5, 2015 @ 20:18

    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

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum