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:/$
Leave a Reply