• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

sed : find the pattern (keyword) and delete the line from file

July 6, 2014 by Sharad Chhetri Leave a Comment

sed is stream line editor utility in unix operating systems.sed is a line-oriented text processing utility: it reads text, line by line, from an input stream or file, into an internal buffer called the pattern space. Each line read starts a cycle. To the pattern space, sed applies one or more operations which have been specified via a sed script .

Knowledge of regex is must for using sed command, if you really want to master on it.

Now we will find the pattern (keyword) and delete the line from file by using sed command.

Syntax:

sed '/PATTERN-or-KEYWORD/d' /path/of/file

Example: We want to delete the line, wherever the red keyword is present.

Content of file is given below

sharad@linuxworld:/$ cat /tmp/test 
Writing a letter with blue pen
under the green tree.
Red roses were planted in garden
Oranges on tree looks beautiful
What else , just learn the sed command
sharad@linuxworld:/$

Use the sed command to remove lines where keyword red is present.

sed '/Red/d' /tmp/test

Below is the output, but it will not edit the file.

sharad@linuxworld:/$ sed '/Red/d' /tmp/test
Writing a letter with blue pen
under the green tree.
Oranges on tree looks beautiful
What else , just learn the sed command
sharad@linuxworld:/$ 

You can also save its output in some file by using > directive.

sed '/Red/d' /tmp/test > output_file

To edit the file with sed command.Use the flag -i with sed command

Here,
-i = edit files in place

Use the below given command for edit the file and deleting the line after finding the keyword

sed -i '/Red/d' /tmp/test

See the below given output reference from my system

sharad@linuxworld:/$ sed -i '/Red/d' /tmp/test
sharad@linuxworld:/$ cat /tmp/test
Writing a letter with blue pen
under the green tree.
Oranges on tree looks beautiful
What else , just learn the sed command
sharad@linuxworld:/$

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. How to use grep command to get fixed pattern or exact keyword
  2. How to print particular line number by using sed command
  3. cat,sed and awk commands to display file contents
  4. search keyword in file with line number using grep command
  5. awk command to search keyword or strings in file
  6. Convert new line to space by using sed command
  7. grep command to find multiple strings or keyword from file
  8. Taking file backup while using sed command with -i flag
  9. remove html tags by using sed command
  10. sed command to display text between two strings or keywords

Filed Under: Linux, Linux Commands Tagged With: linux command, sed

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Linux Command

What is Linux Internal And External Command

Linux Basic Commands With Examples For Every Beginner

tr command to convert lines to space , tab and vertical tab

smbpasswd command not found on CentOS 7 and RHEL 7

Solution : semanage command not found

Unix / Linux : How to print duplicate lines from file

More Posts from this Category

You Might Like These Articles!

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

libfuse

dlopen(): error loading libfuse.so.2 – Got Error On Ubuntu

Failed to open/create the internal network

VirtualBox Error: Failed to open/create the internal network

Always Useful Tips And Tricks

rsync all files,hidden files,symlinks,hardlinks to remotes Linux Server

df command not showing correct free space in linux

How to configure ethernet in CentOS 6 after installing in Virtual Box

Do not show line haveing particular keyword by grep command

Install libjpegtran by using yum command in CentOS 6.x

How to convert float to integer number

Set GRUB password after installation of CentOS/Red Hat

Explore 90+ Article On "Linux Tips And Tricks"

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy