SED Tutorial - how print or delete particular line

SED Tutorial - Part 1

In this article of sed tutorial series, we are going to see how to delete or remove a particular line or a particular pattern from a file using the sed command.

Let us consider a file with the sample contents as below:

~] cat example.txt 
one Red-Hat is Linux
two red-hat is linux
(3) FreeBSD is Unix
"4" freebsd is Unix
(5) Solaris is Linux
"6" solaris is linux
(7) lubuntu is debian
"8" kubuntu is debian
(9) kubuntu is linux 

#11 xubuntu is debian

Delete line N in file


The d option in sed command is used to delete a line. The syntax for deleting a line is: sed 'Nd' file

delete first line in file

~] sed '1d' example.txt 
two red-hat is linux
(3) FreeBSD is Unix
"4" freebsd is Unix
(5) Solaris is Linux
"6" solaris is linux
(7) lubuntu is debian
"8" kubuntu is debian
(9) kubuntu is linux 

#11 xubuntu is debian

delete 4th line in file

~] sed '4d' example.txt  
one Red-Hat is Linux
two red-hat is linux
(3) FreeBSD is Unix
(5) Solaris is Linux
"6" solaris is linux
(7) lubuntu is debian
"8" kubuntu is debian
(9) kubuntu is linux 

#11 xubuntu is debian

delete last line

The following sed command is used to remove the footer line in a file. The $ indicates the last line of a file.

~] sed '$d' example.txt  
one Red-Hat is Linux
two red-hat is linux
(3) FreeBSD is Unix
"4" freebsd is Unix
(5) Solaris is Linux
"6" solaris is linux
(7) lubuntu is debian
"8" kubuntu is debian
(9) kubuntu is linux 

Delete range of lines


The syntax is: sed 'm,nd' file. This sed command delete line from m to n.

delete first 4 lines

~] sed '1,4d' example.txt   
(5) Solaris is Linux
"6" solaris is linux
(7) lubuntu is debian
"8" kubuntu is debian
(9) kubuntu is linux 

#11 xubuntu is debian

Delete range of lines from 2 to 5

~] sed '2,5d' example.txt   
one Red-Hat is Linux
"6" solaris is linux
(7) lubuntu is debian
"8" kubuntu is debian
(9) kubuntu is linux 

#11 xubuntu is debian

Delete lines from 4 to end of file

~] sed '4,$d' example.txt 
one Red-Hat is Linux
two red-hat is linux
(3) FreeBSD is Unix

Delete last n lines

tac file | sed -i '1,nd' | tac

tac reverses the file, sed deletes d the lines from 1 to n numbers of lines

delete last 5 lines

~] tac example.txt | sed '1,5d' | tac  
one Red-Hat is Linux
two red-hat is linux
(3) FreeBSD is Unix
"4" freebsd is Unix
(5) Solaris is Linux
"6" solaris is linux

delete lines 2 to 4 from end

~] tac example.txt | sed '2,4d' | tac    
one Red-Hat is Linux
two red-hat is linux
(3) FreeBSD is Unix
"4" freebsd is Unix
(5) Solaris is Linux
"6" solaris is linux
(7) lubuntu is debian
#11 xubuntu is debian

delete linex 3 to 7 only if contains case insensitive string linux

For case insensitive search you must include I before delete d command

{/regex/} - is regular expression we want to condtition search

~] sed '3,7{/Linux/Id}' example.txt     
one Red-Hat is Linux
two red-hat is linux
(3) FreeBSD is Unix
"4" freebsd is Unix
(7) lubuntu is debian
"8" kubuntu is debian
(9) kubuntu is linux

#11 xubuntu is debian

Specify the list of lines you want to remove


You can specify the list of lines you want to remove in sed command with semicolon as a delimiter.

delete 2th and 4th line in file

~] sed '2d;4d' example.txt     
one Red-Hat is Linux
(3) FreeBSD is Unix
(5) Solaris is Linux
"6" solaris is linux
(7) lubuntu is debian
"8" kubuntu is debian
(9) kubuntu is linux 

#11 xubuntu is debian

delete 2th line, line from 4 to 6 and 8th line

~] sed '2d;4,6d;8d' example.txt      
one Red-Hat is Linux
(3) FreeBSD is Unix
(7) lubuntu is debian
(9) kubuntu is linux 

#11 xubuntu is debian

Delete first and last line

~] sed '1d;$d' example.txt           
two red-hat is linux
(3) FreeBSD is Unix
"4" freebsd is Unix
(5) Solaris is Linux
"6" solaris is linux
(7) lubuntu is debian
"8" kubuntu is debian
(9) kubuntu is linux 

Delete lines based on regular expressions


Delete lines starts with some string


we want delete lines that start with string two

~] sed '/^two/d' example.txt                      
one Red-Hat is Linux
(3) FreeBSD is Unix
"4" freebsd is Unix
(5) Solaris is Linux
"6" solaris is linux
(7) lubuntu is debian
"8" kubuntu is debian
(9) kubuntu is linux 

#11 xubuntu is debian

delete lines that start with strings one, or two, or “8”

~] sed '/^one\|two\|"8"/d' example.txt 
(3) FreeBSD is Unix
"4" freebsd is Unix
(5) Solaris is Linux
"6" solaris is linux
(7) lubuntu is debian
(9) kubuntu is linux 

#11 xubuntu is debian

Delete all lines ending with a particular character

We want delete all linex that ends with string linux case insensitive

~] sed '/linux$/Id' example.txt  
(3) FreeBSD is Unix
"4" freebsd is Unix
(7) lubuntu is debian
"8" kubuntu is debian

#11 xubuntu is debian

Delete lines that contain a pattern


We want delete lines that contain string Linux

~] sed '/Linux/d' example.txt               
two red-hat is linux
(3) FreeBSD is Unix
"4" freebsd is Unix
"6" solaris is linux
(7) lubuntu is debian
"8" kubuntu is debian
(9) kubuntu is linux 

#11 xubuntu is debian

Delete lines with case insensitive

You must include I before delete d command

~} sed '/Linux/Id' example.txt 
(3) FreeBSD is Unix
"4" freebsd is Unix
(7) lubuntu is debian
"8" kubuntu is debian

#11 xubuntu is debian

Delete lines starting from a pattern till the last line

~] sed '/3/,$d' example.txt          
one Red-Hat is Linux
two red-hat is linux

Delete lines starting from a pattern till to another pattern

Delete lines from line contain char 3 till to line that contain char 7

~] sed '/3/,/7/d' example.txt  
one Red-Hat is Linux
two red-hat is linux
"8" kubuntu is debian
(9) kubuntu is linux 

#11 xubuntu is debian

Delete lines from line contain char 3 till to line that contain char 7 only if line contains string FreeBSD

~} sed '/3/,/7/{/FreeBSD/d}' example.txt 
one Red-Hat is Linux
two red-hat is linux
"4" freebsd is Unix
(5) Solaris is Linux
"6" solaris is linux
(7) lubuntu is debian
"8" kubuntu is debian
(9) kubuntu is linux 

#11 xubuntu is debian

Delete lines from line contain char 3 till to line that contain char 7 only if line contains string Linux witch case insensitive

~] sed '/3/,/7/{/Linux/Id}' example.txt 
one Red-Hat is Linux
two red-hat is linux
(3) FreeBSD is Unix
"4" freebsd is Unix
(7) lubuntu is debian
"8" kubuntu is debian
(9) kubuntu is linux 

#11 xubuntu is debian

With the sed command, we can specify the starting pattern and the ending pattern, to print the lines between strings with these patterns. The syntax and the example are shown below.

sed -n '/StartPattern/,/EndPattern/p' FileName
Option Description
-n, –quiet, –silent Suppress automatic printing of pattern space
-p Print the current pattern space

example:

~] sed -n '/FreeBSD/,/lubuntu/p' example.txt  
(3) FreeBSD is Unix
"4" freebsd is Unix
(5) Solaris is Linux
"6" solaris is linux
(7) lubuntu is debian

or with ! negation character print opaque

~] sed -n '/FreeBSD/,/lubuntu/!p' example.txt
one Red-Hat is Linux
two red-hat is linux
"8" kubuntu is debian
(9) kubuntu is linux

#11 xubuntu is debian

Negation in SED - Delete lines other than


Use the negation ! operator with d option in sed command.

removes all lines except first line

Or another words print only first line:

~] sed '1!d' example.txt   
one Red-Hat is Linux

Delete lines other than firt 4 lines

Or another words: print only first 4 lines

~] sed '1,4!d' example.txt
one Red-Hat is Linux
two red-hat is linux
(3) FreeBSD is Unix
"4" freebsd is Unix

Delete lines other than lines from 2 to 4

Or another words: print only lines from 2 to 4

~] sed '2,4!d' example.txt 
two red-hat is linux
(3) FreeBSD is Unix
"4" freebsd is Unix

Delete lines other than last line

Or another word: print only last line:

~} sed '$!d' example.txt   
#11 xubuntu is debian

Delete lines other than from 5th line to last line

Or another words: print only lines from 5 line to end of file:

~] sed '5,$!d' example.txt
(5) Solaris is Linux
"6" solaris is linux
(7) lubuntu is debian
"8" kubuntu is debian
(9) kubuntu is linux

#11 xubuntu is debian

How print particular lines in SED


We want delete all lines except from first to N line. The syntax is: sed '1,N!d'

print only first two lines

~] sed '1,2!d' example.txt     
one Red-Hat is Linux
two red-hat is linux
~] sed '$!d' example.txt   
#11 xubuntu is debian

We want delete line 1,2,3 and lines from 5 to end of file:

~] sed '1,3d;6,$d' example.txt   
"4" freebsd is Unix
(5) Solaris is Linux

Assume, we need print only lines that begin with number string two. We need delete all lines other that lines start with string two:

~] sed '/^two/!d' example.txt   
two red-hat is linux
~] sed '/two/,/6/!d' example.txt         
two red-hat is linux
(3) FreeBSD is Unix
"4" freebsd is Unix
(5) Solaris is Linux
"6" solaris is linux
Share Comments
comments powered by Disqus