Unix utility SED provides an effective and versatile way of deleting one or more lines from a designated file to match the needs of the user. This Unix command is used for command line processing.
What is the purpose of SED utility?
This utility can be used to delete expressions from a file which can be identified by a specifying delimiter (such as a comma, tab, or space), by line number, or by searching for a string, expression or the address of a line in the syntax of Sed.
How to delete one or more lines from a file via SED?
Here is how to remove one or more lines from a file.
Syntax
sed '{[/]<n>|<string>|<regex>[/]}d' <fileName> sed '{[/]<adr1>[,<adr2>][/]d' <fileName>
/.../=delimiters
n = line number
string = string found in in line
regex = regular expression corresponding to the searched pattern
addr = address of a line (number or pattern )
d = delete
Examples of Sed
Here are some examples of how to use the above syntax.
Use the following code to remove the third line:
sed '3d' fileName.txt
Remove the line containing the string "awk," by using:
sed '/awk/d' filename.txt
You can remove the last line by typing in:
sed '$d' filename.txt
Or remove all empty lines through:
sed '/^$/d' filename.txt sed '/./!d' filename.txt
Remove the line matching by a regular expression (by eliminating one containing digital characters, at least 1 digit, located at the end of the line):
sed '/[0-9/][0-9]*$/d' filename.txt
Remove the interval between lines 7 and 9:
sed '7,9d' filename.txt
The same operation as above but replacing the address with parameters:
sed '/-Start/,/-End/d' filename.txt
The above examples are only changed at the display of the file (stdout1= screen).
For permanent changes to the old versions (<4) use a temporary file for GNU sed using the "-i[suffix]":
sed -i".bak" '3d' filename.txt
Linux
- Sed delete line
- Sed remove line
- Bash remove line from file
- Shortcut key to delete > Guide
- Delete fake facebook account within 24 hours [solved] > Forum - Facebook
- This message was deleted text symbol > Forum - Android
- How to delete whatsapp contact not in address book > Guide
- Why can't i delete my history on google chrome > Guide
- How to compile assembly code: in Linux, Windows, with NASM
- Search in Vim editor: Linux, for words
- Ubuntu remove password: for sudo, for user, for root
- How to disable avahi-daemon: Linux, Ubuntu
- Change password Linux: script, terminal, Samba
- Debian add printer: command line, install cups
- Debian change language: to English, command
- Linux read file line by line: for loop
- How to uninstall package: in Linux
- How to resize partition Linux: Gparted, command line
- Vim autocomplete: C++, command
- Linux display file in reverse order
- How to recover lost admin password in CUPS?
- Restart network interface: Linux, Ubuntu
- How to install htop Linux: Ubtunu, Mint
- How to create commands in Linux?
- Customize Wine on Linux: download, install, locate theme
- How to split a file in Linux: split, merge
- What is a Daemon: in Linux
- How to convert DOS to UNIX: Linux, command
- How to remove the content of a directory: Linux
- Adding space in mount point: to FSTAB
- Restoring Linux: from backup, using Tar