{"id":8680,"date":"2021-07-30T08:58:10","date_gmt":"2021-07-30T08:58:10","guid":{"rendered":"https:\/\/linuxways.net\/?p=8680"},"modified":"2021-07-30T08:58:10","modified_gmt":"2021-07-30T08:58:10","slug":"usage-of-sed-command-to-delete-a-single-line-and-multiple-lines-in-linux-mint-20","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/mint\/usage-of-sed-command-to-delete-a-single-line-and-multiple-lines-in-linux-mint-20\/","title":{"rendered":"Usage of \u201csed\u201d Command to Delete a Single Line and Multiple Lines in Linux Mint 20"},"content":{"rendered":"<h2>Introduction:<\/h2>\n<p>The \u201csed\u201d command in Linux is a multi-purpose command that is used to substitute, find, replace, insert, delete, and search text in files. \u201csed\u201d, in fact, stands for Stream Editor. In this article, we intend to explore the usage of this command for deleting a single line as well as multiple lines from a file in Linux Mint 20.<\/p>\n<p>However, before proceeding with these methods, we would like to share with you the general syntax of this command which is as follows:<\/p>\n<pre><strong>$ sed [option] [filename]<\/strong><\/pre>\n<p>Also, for all the methods discussed below, we will be using the same sample text file named List.txt. The contents of our sample file are shown below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"393\" height=\"229\" class=\"wp-image-8681\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-664.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-664.png 393w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-664-300x175.png 300w\" sizes=\"auto, (max-width: 393px) 100vw, 393px\" \/><\/p>\n<h2>Methods of Deleting a Single Line using the \u201csed\u201d Command in Linux Mint 20:<\/h2>\n<p>If you want to delete a single line from a file using the \u201csed\u201d command, then depending on its position, you can choose one of the following two methods:<\/p>\n<h3>Method # 1: Deleting any Single Line other than the Last One:<\/h3>\n<p>For deleting any line from your file except for the last one, you will have to run the \u201csed\u201d command in the manner shown below:<\/p>\n<pre><strong>$ sed \u2018NUMd\u2019 filename<\/strong><\/pre>\n<p>Here, you need to replace NUM with the line number of the line that you want to delete for example \u20181d\u2019 for the first line of the file followed by the exact name of that file.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"387\" height=\"25\" class=\"wp-image-8682\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-665.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-665.png 387w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-665-300x19.png 300w\" sizes=\"auto, (max-width: 387px) 100vw, 387px\" \/><\/p>\n<p>You can see from the following image that the first line of our original text file has been deleted.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"390\" height=\"209\" class=\"wp-image-8683\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-666.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-666.png 390w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-666-300x161.png 300w\" sizes=\"auto, (max-width: 390px) 100vw, 390px\" \/><\/p>\n<h3>Method # 2: Deleting the Last Line:<\/h3>\n<p>Now, if you want to delete the last line of your file, then you will have to run the \u201csed\u201d command in the manner shown below:<\/p>\n<pre><strong>$ sed \u2018$d\u2019 filename<\/strong><\/pre>\n<p>Here, the \u2018$\u2019 symbol corresponds to the last line of the specified file.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"380\" height=\"23\" class=\"wp-image-8684\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-667.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-667.png 380w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-667-300x18.png 300w\" sizes=\"auto, (max-width: 380px) 100vw, 380px\" \/><\/p>\n<p>You can see from the output shown in the following image that the last line of our original text file has been deleted.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"436\" height=\"213\" class=\"wp-image-8685\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-668.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-668.png 436w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-668-300x147.png 300w\" sizes=\"auto, (max-width: 436px) 100vw, 436px\" \/><\/p>\n<h2>Methods of Deleting Multiple Lines using the \u201csed\u201d Command in Linux Mint 20:<\/h2>\n<p>If you want to delete multiple lines from a file using the \u201csed\u201d command, then depending on your exact need, you can choose one of the following three methods:<\/p>\n<h3>Method # 1: Deleting a Range of Lines:<\/h3>\n<p>For deleting a specified range of lines, you will have to execute the \u201csed\u201d command in the manner shown below:<\/p>\n<pre><strong>$ sed \u2018NUM, NUM2d\u2019 filename<\/strong><\/pre>\n<p>Here, NUM represents the starting line number for deletion whereas NUM2 represents the ending line number for deletion. We wanted to delete the lines 1-3 from our specified file so we replaced NUM with 1 and NUM2 with 3.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"401\" height=\"25\" class=\"wp-image-8686\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-669.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-669.png 401w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-669-300x19.png 300w\" sizes=\"auto, (max-width: 401px) 100vw, 401px\" \/><\/p>\n<p>You can see from the output shown in the following image that the lines 1-3 have been successfully deleted from our file.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"434\" height=\"173\" class=\"wp-image-8687\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-670.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-670.png 434w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-670-300x120.png 300w\" sizes=\"auto, (max-width: 434px) 100vw, 434px\" \/><\/p>\n<h3>Method # 2: Deleting Some Specific Lines:<\/h3>\n<p>If you do not want to delete an entire range of lines rather you want to pick some lines at random for deletion, then you will have to execute the \u201csed\u201d command in the manner shown below:<\/p>\n<pre><strong>$ sed \u2018NUMd;NUM2d; NUM3d;\u2026.\u2019 filename<\/strong><\/pre>\n<p>Here, NUM, NUM2, and NUM3 refer to the line numbers of all the different lines that you want to delete.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"435\" height=\"25\" class=\"wp-image-8688\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-671.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-671.png 435w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-671-300x17.png 300w\" sizes=\"auto, (max-width: 435px) 100vw, 435px\" \/><\/p>\n<p>You can see from the following image that our specified lines have been successfully deleted.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"446\" height=\"181\" class=\"wp-image-8689\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-672.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-672.png 446w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-672-300x122.png 300w\" sizes=\"auto, (max-width: 446px) 100vw, 446px\" \/><\/p>\n<h3>Method # 3: Deleting all the Lines except for the Specified Range:<\/h3>\n<p>Finally, if you want to delete all the lines from a file except for a specified range, then you will have to execute the \u201csed\u201d command in the manner shown below:<\/p>\n<pre><strong>$ sed \u2018NUM, NUM2!d\u2019 filename<\/strong><\/pre>\n<p>Here, NUM represents the starting line number and NUM2 represents the ending line number of the range of lines that you want to prevent from deletion.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"408\" height=\"25\" class=\"wp-image-8690\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-673.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-673.png 408w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-673-300x18.png 300w\" sizes=\"auto, (max-width: 408px) 100vw, 408px\" \/><\/p>\n<p>You can see from the following image that all other lines except for the specified range have been successfully deleted from our file.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"406\" height=\"91\" class=\"wp-image-8691\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-674.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-674.png 406w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-674-300x67.png 300w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/p>\n<h2>Conclusion:<\/h2>\n<p>This article explained to you one of the many uses of the \u201csed\u201d command in Linux Mint 20. By using this command in the ways described in this article, you will be able to delete lines from any file on Linux very conveniently.<\/p>","protected":false},"excerpt":{"rendered":"<p>Introduction: The \u201csed\u201d command in Linux is a multi-purpose command that is used to substitute, find, replace, insert, delete, and search text in files. \u201csed\u201d, in fact, stands&hellip;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[129,172],"class_list":["post-8680","post","type-post","status-publish","format-standard","hentry","category-mint","tag-linux-mint-20","tag-sed-command"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/8680","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=8680"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/8680\/revisions"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=8680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=8680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=8680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}