{"id":770,"date":"2020-08-22T21:44:41","date_gmt":"2020-08-22T16:14:41","guid":{"rendered":"https:\/\/techieshouts.com\/?p=770"},"modified":"2022-08-09T19:05:21","modified_gmt":"2022-08-09T13:35:21","slug":"delete-first-line-from-file-in-shell-script","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/delete-first-line-from-file-in-shell-script\/","title":{"rendered":"Delete first line from file in shell script"},"content":{"rendered":"\n<p>We can delete the first line in a file using &#8216;sed&#8217; command in the shell script. Let us see with the help of a test file called &#8216;sample.txt&#8217;. Here are the contents of the text file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ cat sample.txt\nfirst line\nsecond line\nthird line<\/pre>\n\n\n\n<h2>Option 1: Delete and save in a new file<\/h2>\n\n\n\n<p>In this option, a new file will be created with the modified data and the original file will remain the same. <\/p>\n\n\n\n<p>sed &#8211; stream editor, &#8216;1d&#8217; &#8211; delete first line. If you want to delete the <strong>first two lines, use &#8216;1,2d&#8217;<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sed '1d' sample.txt > newfile.txt<\/pre>\n\n\n\n<p>Here, the original file sample.txt has 3 lines and the new file newfile.txt will have only two lines.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ cat sample.txt\nfirst line\nsecond line\nthird line\n$ cat newfile.txt\nsecond line\nthird line<\/pre>\n\n\n\n<h2>Option 2: Delete and save in the same file<\/h2>\n\n\n\n<p>In this option, the content will be deleted in the same file. The command should have an extra &#8216;-i&#8217; after &#8216;sed&#8217;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sed -i '1d' sample.txt<\/pre>\n\n\n\n<p>The above command will delete the first line in the sample.txt file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ cat sample.txt\nsecond line\nthird line<\/pre>\n\n\n\n<p><strong>NOTE:<\/strong> If you are running this in Mac, you need to add &#8216;-e&#8217; after &#8216;-i&#8217;. The command will look like <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sed -i -e '1d' sample.txt<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We can delete the first line in a file using &#8216;sed&#8217; command in the shell script. Let us see with the help of a test file called &#8216;sample.txt&#8217;. Here are the contents of the text file. Option 1: Delete and save in a new file In this option, a new file will be created with\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/delete-first-line-from-file-in-shell-script\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,8],"tags":[89,88,87,86],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/770"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=770"}],"version-history":[{"count":16,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/770\/revisions"}],"predecessor-version":[{"id":789,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/770\/revisions\/789"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}