[Sed] Delete one or more lines from a file

Last update on January 12, 2009 08:56 AM by jak58
Published by jak58

[Sed] Delete one or more lines from a file








Removing one (or several) line (s) of 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

Remove the 3rd line:

sed '3d' fileName.txt


Removal of the line containing the string "awk":

sed '/awk/d' filename.txt


Remove the last line:

sed '$d' filename.txt



Remove all empty lines:

sed '/^$/d' filename.txt
sed '/./!d' filename.txt


Remove line "matched" 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


Removing the interval between lines 7 and 9.

sed '7,9d' filename.txt 


Same operation but replacing the address by "parameters".

sed '/-Start/,/-End/d' filename.txt



The above examples are only change at the display of the file (stdout1= screen).

For permanent changes to the old versions (<4) use a temporary file for GNU sed use the "-i[suffix]":

sed -i".bak" '3d' filename.txt
Best answers for « Delete one or more lines from a file » in :
How to restore deleted files ShowHow to restore deleted files Download a recovery tool There are easy and effective recovery tools available for download on the web and some have the major advantage of being free. Zero Assumption Recovery Download Link: http://www.z-a...
Assembling multiple PDF files ShowAssembling multiple PDF files Intro Steps: Intro PDF creator is a software allowing you to print several files into one with its printer function. Download link http://en.kioskea.net/telecharger/telecharger-40-pdf...
Delete SP1 uninstall files on Windows Vista ShowDelete SP1 uninstall files on Vista Uninstall service pack by using the Programs and Features item in Control Panel Uninstall Service Pack using the Command This tip will allow you to save more than 700 Mb on the system partition by...
Download Snap Links ShowDuring an internet search, you normally obtain more than one result at the time. It is not always easy to open all the links one by one. Snap Links is a Firefox extension allowing to select several links at the same time and open them simultaneously....
Download Locked Files Wizard (ex CopyLock) ShowA program, a folder or a fil used by a process can't be moved or erased. Locked Files Wizard (LFW) is a wizard allowing to move, delete or rename a file or a folder, even if it's used by a process. This program also allows to stop running process...
UNIX Commands ShowTable of the main UNIX commands Unix Commands Description Options ls lists the content of a directory -a Displays all files, including hidden files -I Displays a...
Linux - The shell ShowIntroduction to the shell The command interpreter is the interface between the user and the operating system, hence the name "shell". The shell therefore acts as an intermediary between the operating system and the user thanks to command lines...
The file system ShowWhat is a file system? Even though hard drives can be very small, they still contain millions of bits and therefore need to be organised so that information can be located. This is the purpose of the file system. Remember that a hard drive is made...