Friday, 15 January 2016

How to Delete Lines Using Sed Command In Unix/Linux

In this article we will see how to use d switch of sed command to delete lines from a file. Let see some examples to have more understanding.

Delete A Particular Line From A File

$ sed '3d' F_Input_File.txt

Output: Will delete 3rd line form the file.
Delete Lines Between A Provided Range

$ sed 'm,nd' InputFile
$ sed '3,9d' F_Input_File.txt
Output: Will delete all the file in the range 3 to 9.
Delete Lines Containing A Required Word

$ sed '/knowledge/d' F_Input_File.txt

Output: Will delete lines with word knowledge
Delete Lines Between Two Words

$ sed '/knowledge/,/warehouse/d' F_Input_File.txt

Output: Will delete all the lines in between the lines which have word knowledge & warehouse in it.
Delete The Last Line

$ sed '$d' F_Input_File.txt

Output: Will delete the last line from the file.
$ indicates the last line of the file
Delete Empty Lines

$ sed '^$d' F_Input_File.txt

Output: Will delete the empty lines from the file.

Delete All The Lines Except Header

$ sed '1!d' F_Input_File.txt

Output: Will keep the first line/header and delete rest of the lines.
Delete The Lines Other Than Provided Range

$ sed '1,5!d' F_Input_File.txt

Output: Will delete all the line except 1 to 5.
Delete Multiple Specific Lines

$ sed '2d;4d;7d' F_Input_File.txt

Output: Will delete 2nd, 4th & 7th line from the input file.
Keep Reading, Keep Learning, Keep Sharing..!!

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...