{"id":3701,"date":"2023-02-09T18:14:32","date_gmt":"2023-02-09T18:14:32","guid":{"rendered":"https:\/\/www.webhi.com\/how-to\/?p=3701"},"modified":"2023-02-09T18:14:35","modified_gmt":"2023-02-09T18:14:35","slug":"guide-to-using-the-sed-command-in-linux","status":"publish","type":"post","link":"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/","title":{"rendered":"Guide to using the &#8220;sed&#8221; command in Linux"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/www.webhi.com\/how-to\/gilrogre\/2023\/02\/sed_linux_en.jpg\" alt=\"Using the 'sed' command in Linux: A step-by-step guide with examples\" class=\"wp-image-3758\" srcset=\"https:\/\/www.webhi.com\/how-to\/gilrogre\/2023\/02\/sed_linux_en.jpg 1200w, https:\/\/www.webhi.com\/how-to\/gilrogre\/2023\/02\/sed_linux_en-300x169.jpg 300w, https:\/\/www.webhi.com\/how-to\/gilrogre\/2023\/02\/sed_linux_en-1024x576.jpg 1024w, https:\/\/www.webhi.com\/how-to\/gilrogre\/2023\/02\/sed_linux_en-768x432.jpg 768w, https:\/\/www.webhi.com\/how-to\/gilrogre\/2023\/02\/sed_linux_en-150x84.jpg 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<p><strong><code>sed<\/code> <\/strong>is a stream editor in Linux that is used to perform basic text transformations on an input stream (a file or input from a pipeline). This guide will provide examples of how to use the <code>sed<\/code> command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p>The basic syntax of the <code>sed<\/code> command is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">$ sed [options] 'command' file<\/code><\/pre>\n\n\n\n<ul><li><code>options<\/code>: optional flags to modify the behavior of the <code>sed<\/code> command.<\/li><li><code>command<\/code>: the text transformation to be performed.<\/li><li><code>file<\/code>: the input file to be edited (or omitted for standard input).<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Usage<\/h2>\n\n\n\n<h5 class=\"wp-block-heading\">Replacing text<\/h5>\n\n\n\n<p>One of the most common uses of <code>sed<\/code> is to replace text in a file. To replace a string in a file, the following syntax is used:<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">$ sed 's\/old-text\/new-text\/g' file<\/code><\/pre>\n\n\n\n<p>The <code>s<\/code> indicates that we are using the substitution command, <code>old-text<\/code> is the text to be replaced, <code>new-text<\/code> is the text to replace it with, and the <code>g<\/code> at the end indicates that all occurrences of <code>old-text<\/code> should be replaced (without <code>g<\/code>, only the first occurrence in each line would be replaced).<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Printing specific lines<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">$ sed -n '3,5p' file<\/code><\/pre>\n\n\n\n<p>This will print lines 3 to 5 from the file. The <code>-n<\/code> option tells <code>sed<\/code> to only print the specified lines and not all lines in the file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced Usage<\/h2>\n\n\n\n<h5 class=\"wp-block-heading\">Using Regular Expressions<\/h5>\n\n\n\n<p><code>sed<\/code> supports basic regular expressions, which can be used to match and replace text based on patterns. The following example shows how to use a regular expression to replace all occurrences of a number in a file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">$ sed 's\/[0-9]\/X\/g' file<\/code><\/pre>\n\n\n\n<p>This will replace all occurrences of numbers in the file with the letter <code>X<\/code>.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Deleting Lines<\/h5>\n\n\n\n<p>You can use <code>sed<\/code> to delete specific lines from a file. The following syntax will delete lines 3 to 5:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">$ sed '3,5d' file<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Inserting Lines<\/h5>\n\n\n\n<p>You can also insert new lines into a file using <code>sed<\/code>. The following syntax will insert a new line after line 3:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">$ sed '3a\\This is a new line' file<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Appending Text<\/h5>\n\n\n\n<p>You can append text to the end of a file using <code>sed<\/code>. The following syntax will append the text <code>This is some text<\/code> to the end of the file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">$ sed '$a\\This is some text' file<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this guide, we covered the basic and advanced usage of the <code>sed<\/code> command in Linux. With these examples, you should be able to perform a variety of text transformations on your files.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>sed is a stream editor in Linux that is used to perform basic text transformations on an input stream (a file or input from a pipeline). This guide will provide examples of how to use the sed command. Syntax The basic syntax of the sed command is as follows: options: optional flags to modify the ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[69],"tags":[],"yoast_head":"\n<title>Guide to using the &quot;sed&quot; command in Linux - WebHi Tutorials &amp; Documentations<\/title>\n<meta name=\"description\" content=\"Learn the basics of the &#039;sed&#039; command in Linux with examples. This guide covers how to replace, print, delete and add text in a file using the &#039;sed&#039; command.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Guide to using the &quot;sed&quot; command in Linux - WebHi Tutorials &amp; Documentations\" \/>\n<meta property=\"og:description\" content=\"Learn the basics of the &#039;sed&#039; command in Linux with examples. This guide covers how to replace, print, delete and add text in a file using the &#039;sed&#039; command.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/\" \/>\n<meta property=\"og:site_name\" content=\"WebHi Tutorials &amp; Documentations\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webhi.technology\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-09T18:14:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-09T18:14:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webhi.com\/how-to\/gilrogre\/2023\/02\/sed_linux_en.jpg\" \/>\n<meta name=\"author\" content=\"webhi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@WebHiTechnology\" \/>\n<meta name=\"twitter:site\" content=\"@WebHiTechnology\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"webhi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/\"},\"author\":{\"name\":\"webhi\",\"@id\":\"https:\/\/www.webhi.com\/how-to\/#\/schema\/person\/b31e76e2311cdc0bb90f5e2733059a54\"},\"headline\":\"Guide to using the &#8220;sed&#8221; command in Linux\",\"datePublished\":\"2023-02-09T18:14:32+00:00\",\"dateModified\":\"2023-02-09T18:14:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/\"},\"wordCount\":337,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webhi.com\/how-to\/#organization\"},\"articleSection\":[\"Linux system administration\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/\",\"url\":\"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/\",\"name\":\"Guide to using the \\\"sed\\\" command in Linux - WebHi Tutorials &amp; Documentations\",\"isPartOf\":{\"@id\":\"https:\/\/www.webhi.com\/how-to\/#website\"},\"datePublished\":\"2023-02-09T18:14:32+00:00\",\"dateModified\":\"2023-02-09T18:14:35+00:00\",\"description\":\"Learn the basics of the 'sed' command in Linux with examples. This guide covers how to replace, print, delete and add text in a file using the 'sed' command.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webhi.com\/how-to\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Guide to using the &#8220;sed&#8221; command in Linux\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webhi.com\/how-to\/#website\",\"url\":\"https:\/\/www.webhi.com\/how-to\/\",\"name\":\"WebHi Tutorials &amp; Documentations\",\"description\":\"System administration and knowledge base\",\"publisher\":{\"@id\":\"https:\/\/www.webhi.com\/how-to\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webhi.com\/how-to\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webhi.com\/how-to\/#organization\",\"name\":\"WebHi Technology\",\"url\":\"https:\/\/www.webhi.com\/how-to\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webhi.com\/how-to\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webhi.com\/how-to\/gilrogre\/2022\/07\/logo.png\",\"contentUrl\":\"https:\/\/www.webhi.com\/how-to\/gilrogre\/2022\/07\/logo.png\",\"width\":288,\"height\":95,\"caption\":\"WebHi Technology\"},\"image\":{\"@id\":\"https:\/\/www.webhi.com\/how-to\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webhi.technology\",\"https:\/\/twitter.com\/WebHiTechnology\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webhi.com\/how-to\/#\/schema\/person\/b31e76e2311cdc0bb90f5e2733059a54\",\"name\":\"webhi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webhi.com\/how-to\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/www.webhi.com\/how-to\/ahuphiph\/litespeed\/avatar\/e20da107d0f4c765ead2eef88ad019d8.jpg?ver=1780005063\",\"contentUrl\":\"https:\/\/www.webhi.com\/how-to\/ahuphiph\/litespeed\/avatar\/e20da107d0f4c765ead2eef88ad019d8.jpg?ver=1780005063\",\"caption\":\"webhi\"},\"sameAs\":[\"https:\/\/www.webhi.com\/how-to\"],\"url\":\"https:\/\/www.webhi.com\/how-to\/author\/webhi\/\"}]}<\/script>\n","yoast_head_json":{"title":"Guide to using the \"sed\" command in Linux - WebHi Tutorials &amp; Documentations","description":"Learn the basics of the 'sed' command in Linux with examples. This guide covers how to replace, print, delete and add text in a file using the 'sed' command.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/","og_locale":"en_US","og_type":"article","og_title":"Guide to using the \"sed\" command in Linux - WebHi Tutorials &amp; Documentations","og_description":"Learn the basics of the 'sed' command in Linux with examples. This guide covers how to replace, print, delete and add text in a file using the 'sed' command.","og_url":"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/","og_site_name":"WebHi Tutorials &amp; Documentations","article_publisher":"https:\/\/www.facebook.com\/webhi.technology","article_published_time":"2023-02-09T18:14:32+00:00","article_modified_time":"2023-02-09T18:14:35+00:00","og_image":[{"url":"https:\/\/www.webhi.com\/how-to\/gilrogre\/2023\/02\/sed_linux_en.jpg"}],"author":"webhi","twitter_card":"summary_large_image","twitter_creator":"@WebHiTechnology","twitter_site":"@WebHiTechnology","twitter_misc":{"Written by":"webhi","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/#article","isPartOf":{"@id":"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/"},"author":{"name":"webhi","@id":"https:\/\/www.webhi.com\/how-to\/#\/schema\/person\/b31e76e2311cdc0bb90f5e2733059a54"},"headline":"Guide to using the &#8220;sed&#8221; command in Linux","datePublished":"2023-02-09T18:14:32+00:00","dateModified":"2023-02-09T18:14:35+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/"},"wordCount":337,"commentCount":0,"publisher":{"@id":"https:\/\/www.webhi.com\/how-to\/#organization"},"articleSection":["Linux system administration"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/","url":"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/","name":"Guide to using the \"sed\" command in Linux - WebHi Tutorials &amp; Documentations","isPartOf":{"@id":"https:\/\/www.webhi.com\/how-to\/#website"},"datePublished":"2023-02-09T18:14:32+00:00","dateModified":"2023-02-09T18:14:35+00:00","description":"Learn the basics of the 'sed' command in Linux with examples. This guide covers how to replace, print, delete and add text in a file using the 'sed' command.","breadcrumb":{"@id":"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.webhi.com\/how-to\/guide-to-using-the-sed-command-in-linux\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webhi.com\/how-to\/"},{"@type":"ListItem","position":2,"name":"Guide to using the &#8220;sed&#8221; command in Linux"}]},{"@type":"WebSite","@id":"https:\/\/www.webhi.com\/how-to\/#website","url":"https:\/\/www.webhi.com\/how-to\/","name":"WebHi Tutorials &amp; Documentations","description":"System administration and knowledge base","publisher":{"@id":"https:\/\/www.webhi.com\/how-to\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webhi.com\/how-to\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webhi.com\/how-to\/#organization","name":"WebHi Technology","url":"https:\/\/www.webhi.com\/how-to\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webhi.com\/how-to\/#\/schema\/logo\/image\/","url":"https:\/\/www.webhi.com\/how-to\/gilrogre\/2022\/07\/logo.png","contentUrl":"https:\/\/www.webhi.com\/how-to\/gilrogre\/2022\/07\/logo.png","width":288,"height":95,"caption":"WebHi Technology"},"image":{"@id":"https:\/\/www.webhi.com\/how-to\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webhi.technology","https:\/\/twitter.com\/WebHiTechnology"]},{"@type":"Person","@id":"https:\/\/www.webhi.com\/how-to\/#\/schema\/person\/b31e76e2311cdc0bb90f5e2733059a54","name":"webhi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webhi.com\/how-to\/#\/schema\/person\/image\/","url":"https:\/\/www.webhi.com\/how-to\/ahuphiph\/litespeed\/avatar\/e20da107d0f4c765ead2eef88ad019d8.jpg?ver=1780005063","contentUrl":"https:\/\/www.webhi.com\/how-to\/ahuphiph\/litespeed\/avatar\/e20da107d0f4c765ead2eef88ad019d8.jpg?ver=1780005063","caption":"webhi"},"sameAs":["https:\/\/www.webhi.com\/how-to"],"url":"https:\/\/www.webhi.com\/how-to\/author\/webhi\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webhi.com\/how-to\/wp-json\/wp\/v2\/posts\/3701"}],"collection":[{"href":"https:\/\/www.webhi.com\/how-to\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webhi.com\/how-to\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webhi.com\/how-to\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webhi.com\/how-to\/wp-json\/wp\/v2\/comments?post=3701"}],"version-history":[{"count":25,"href":"https:\/\/www.webhi.com\/how-to\/wp-json\/wp\/v2\/posts\/3701\/revisions"}],"predecessor-version":[{"id":3773,"href":"https:\/\/www.webhi.com\/how-to\/wp-json\/wp\/v2\/posts\/3701\/revisions\/3773"}],"wp:attachment":[{"href":"https:\/\/www.webhi.com\/how-to\/wp-json\/wp\/v2\/media?parent=3701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webhi.com\/how-to\/wp-json\/wp\/v2\/categories?post=3701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webhi.com\/how-to\/wp-json\/wp\/v2\/tags?post=3701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}