[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 read a file line by line ShowHow to read a file line by line Intro Tips Bonus Intro One of the most common errors of learning scripts bash on GNU / Linux is to read a file line by line, is to use a loop "for" (for line in $ (cat file.txt) do. ..), which in this...
Remove Directory Links from Favorites ShowRemove Directory Links from favourites Hiding Links folder Permanently delete the Links folder Despite deleting the folder "Links" in favorites, this one is regenerated automatically when you restart the computer. There are two...
What to do if a file cannot be deleted ShowWhat to do if a file cannot be deleted How to get rid of the file? Solution 1: Ensure that the file/folder is not in use Solution 2: Run your system on Safe Mode Solution 3: Download or update your Antivirus program Solution 4: Use...
Download Ext2 Installable File System ShowIf you currently have Windows running and you realize that you need some files for your work which you have stored on an Ext2 volume of your Linux installation, you no longer have to shut down Windows and boot Linux! Furthermore, Windows will now...
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 - Files ShowIntroduction to UNIX files In UNIX systems any element is represented in the form of a file. All files are architectured around a single tree structure where the base, called the root, is written "/". File types UNIX systems define different...
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...