Sed - Delete one or more lines from a file

Unix utility SED provides an effective and a 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. This utility can be used to delete expressions from a file which can be identified by a specifying delimiter (like comma, tab, space, etc.), by line number, by searching for a string, expression or the address of a line in the syntax of SED.

[Sed] Delete one or more lines from a file






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

Remove the 3rd line:

sed '3d' fileName.txt


Remove 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 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
Published by jak58 - Last update on February 5, 2012 11:39 PM by Paul Berentzen
This document entitled « Sed - Delete one or more lines from a file » from Kioskea.net (en.kioskea.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the licence, as this note appears clearly.
Suggestions
  •  Sed - Delete one or more lines from a file
  •  [Sed] Tips and tricks » Tips : Sed, or Stream Editor as it is commonly known, is mainly used for parsing text and transforming any text within a script. This feature of Sed is used in Shell scripts to generate flexible and dynamic codes. Knowing the common tips and tricks can help...
  •  How to read a file line by line » Tips : Reading files in the Linux platform uses several programming techniques. The operating system consists of various uses of 'loop' structures like 'while loop' and 'for loop'. With Bash Scripts being the crucial element of reading files in Linux, there...
  •  Inserting text in a file » Tips : To meet programming needs, one may need to insert text in a text file at designated areas. Linux has SED , the utility which allows the programmer to achieve this by following the SED syntax. This enables the user to insert, add or replace text in a...
  •  Retrieve Hidden & locked folders (Solved) » Best answer: hey there, you did wrong while you hard removed the files from the program files you should have removed it in the add or remove programs and after that juts try to reinstall it, it sould let you get the files that you are willing to get. but
  •  Itunes wants to delete all my songs (Solved) » Best answer: hi, if you sync you iPod with iTunes, all the music in the iPod will be deleted. what you need to do is to click on your iPod icon when you connect it to the computer and then check "Manually manage music". then you need to create a playlist,
Converting DOS/UNIX and vice-versa
Inserting a space between each letter