{"id":15984,"date":"2019-03-15T07:54:36","date_gmt":"2019-03-15T07:54:36","guid":{"rendered":"https:\/\/www.hostinger.com\/tutorials\/?p=15984"},"modified":"2025-03-28T08:24:49","modified_gmt":"2025-03-28T08:24:49","slug":"linux-sed-command","status":"publish","type":"post","link":"\/uk\/tutorials\/linux-sed-command","title":{"rendered":"How to Use the Linux sed Command in 2026 With Useful Examples + Free Linux Cheat Sheet"},"content":{"rendered":"<p>The Linux <strong>sed<\/strong> command lets you find, replace, insert, and delete lines in a file without opening it using a text editor. Suitable for piped input and various file formats, this Linux command also uses regular expression or<strong> regex<\/strong>, allowing you to search and manipulate complex patterns.<\/p><p>With its various use cases, understanding how to use sed can help system administrators efficiently manage their virtual private servers (VPSs).<\/p><p>This article will explain the Linux sed command&rsquo;s general syntax, options, and subcommands. You will also learn various sed examples and use cases for managing Linux VPS.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Linux-Commands-Cheat-Sheet.pdf\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" width=\"1024\" height=\"283\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet-1024x283.png\" alt=\"\" class=\"wp-image-69262\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet-1536x425.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet-300x83.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet-150x41.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet-768x212.png 768w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>\n\n\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-sed-command-options\">sed Command Options<\/h2><p>Here&rsquo;s what the sed command&rsquo;s general syntax looks like:<\/p><pre class=\"wp-block-preformatted\">sed options 'script' file_name<\/pre><p>To modify sed commands&rsquo; behavior, add the following command-line options:<\/p><ul class=\"wp-block-list\">\n<li><strong>&ndash;help<\/strong> &ndash; prints command usage information.<\/li>\n\n\n\n<li><strong>&ndash;debug <\/strong>&ndash; enables Terminal to annotate program execution and input.<\/li>\n\n\n\n<li><strong>-i<\/strong> &ndash; overwrites the original file.<\/li>\n\n\n\n<li><strong>-n<\/strong> &ndash; disables automatic printing unless the user uses the <strong>p<\/strong> command.<\/li>\n\n\n\n<li><strong>-u <\/strong>&ndash; minimizes output.<\/li>\n\n\n\n<li><strong>&ndash;posix<\/strong> &ndash; disables POSIX sed extensions to simplify writing portable scripts.<\/li>\n\n\n\n<li><strong>-e<\/strong> &ndash; specifies multiple commands to run sequentially.<\/li>\n\n\n\n<li><strong>-b<\/strong> &ndash; opens input files in binary mode.<\/li>\n\n\n\n<li><strong>-l <\/strong>&ndash; sets the desired line-wrap length for the<strong> l<\/strong> command.<\/li>\n<\/ul><p>The <strong>script <\/strong>contains the subcommand, search pattern, replacement string, and flag. These elements are encapsulated in apostrophes and separated using a delimiter, like a slash (<strong>\/<\/strong>), backslash (<strong>\\<\/strong>), or pipe (<strong>|<\/strong>).<\/p><p>Their order may differ depending on the subcommand. For example, the <strong>s <\/strong>or <strong>substitute <\/strong>command replaces a <a href=\"https:\/\/www.regular-expressions.info\/\" target=\"_blank\" rel=\"noreferrer noopener\">regular expression<\/a> pattern with another string. Here&rsquo;s the syntax:<\/p><pre class=\"wp-block-preformatted\">'s\/regex_pattern\/new_pattern\/flags'<\/pre><p>To alter the pattern substitution, use the following flags:<\/p><ul class=\"wp-block-list\">\n<li><strong>g<\/strong> &ndash; applies global replacement, not just the first occurrence.<\/li>\n\n\n\n<li><strong>Number<\/strong> &ndash; specifies which line numbers to modify.<\/li>\n\n\n\n<li><strong>p<\/strong> &ndash; prints the new line after a successful pattern replacement.<\/li>\n\n\n\n<li><strong>i<\/strong> &ndash; makes the substitution case sensitive.<\/li>\n<\/ul><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Pro Tip<\/h4>\n                    <p>The <strong>s<\/strong> subcommand supports multiple flags. For example, add the <strong>gi<\/strong> flag to enable a global, case-sensitive substitution.<\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-install-sed\">How to Install sed?<\/h2><p>The Linux stream editor package comes pre-installed in most distributions. If your system doesn&rsquo;t have the tool, follow these steps to install it:<\/p><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> In this tutorial, our VPS is running <strong>Ubuntu 22.04<\/strong>. If you use another distribution or version, the commands may differ.<\/p><\/div>\n\n\n\n<\/p><ol class=\"wp-block-list\">\n<li>Connect to your VPS <a href=\"\/uk\/tutorials\/how-to-use-putty-ssh\">using an SSH client like PuTTY<\/a> or Terminal. Hostinger users can use the <strong>Browser terminal<\/strong> via <strong>hPanel<\/strong>.<\/li>\n\n\n\n<li>Enter your root login credentials. On hPanel, they are located in the VPS overview menu&rsquo;s <strong>SSH access tab<\/strong>.<\/li>\n\n\n\n<li>Update the repository by typing this command:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo apt-get update<\/pre><ol start=\"4\" class=\"wp-block-list\">\n<li>Install the <strong>sed package<\/strong> by entering the following command:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo apt-get install sed<\/pre><ol start=\"5\" class=\"wp-block-list\">\n<li>Run the command below to check whether the installation was successful:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sed --version<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-shows-installed-sed-version-1.png\"><img decoding=\"async\" width=\"800\" height=\"42\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-shows-installed-sed-version-1.png\" alt=\"Terminal shows the currently installed sed version\" class=\"wp-image-94267\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/terminal-shows-installed-sed-version-1.png 800w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/terminal-shows-installed-sed-version-1-300x16.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/terminal-shows-installed-sed-version-1-150x8.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/terminal-shows-installed-sed-version-1-768x40.png 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/figure><\/div><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a href=\"\/uk\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner-1024x300.png\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-300x88.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-150x44.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-768x225.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-sed-command-examples\">sed Command Examples<\/h2><p>In the following sections, we will present 10 stream editor command examples to help you understand its functions.<\/p><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> All the commands below don&rsquo;t alter the original file. To apply the changes directly, add the <strong>-i<\/strong> option.<\/p><\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\">Using sed to Search and Replace a String<\/h3><p>The sed command is commonly used for replacing text. This tool will search for the specified pattern in a file and change it with the desired string.<\/p><p>To do so, use the <strong>s <\/strong>command with the string you want to replace and its replacement. Here&rsquo;s the syntax:<\/p><pre class=\"wp-block-preformatted\">sed 's\/old_string\/new_string\/' samplefile.txt<\/pre><p>Change the placeholders with the actual value. For example, this command substitutes the word <strong>&ldquo;images&rdquo; <\/strong>with <strong>&ldquo;photos&rdquo; <\/strong>in the <strong>scenery.txt<\/strong> file:<\/p><pre class=\"wp-block-preformatted\">sed 's\/images\/photos\/' scenery.txt<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/sed-replaced-the-word-images-with-photos.png\"><img decoding=\"async\" width=\"800\" height=\"67\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/sed-replaced-the-word-images-with-photos.png\" alt=\"Terminal output shows sed has replaced the word images with photos\" class=\"wp-image-94269\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-the-word-images-with-photos.png 800w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-the-word-images-with-photos-300x25.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-the-word-images-with-photos-150x13.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-the-word-images-with-photos-768x64.png 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/figure><\/div><p>If your string contains the slash symbol, use another delimiter, like a backslash (<strong>\\<\/strong>) or pipe (|).<\/p><h3 class=\"wp-block-heading\">Using sed to Replace the nth Occurrence of a Pattern in a Line<\/h3><p>If a pattern in a line occurs multiple times, enter the following command syntax to replace a specific one:<\/p><pre class=\"wp-block-preformatted\">sed 's\/old_string\/new_string\/#' samplefile.txt<\/pre><p>Substitute the hash (#) symbol with the pattern&rsquo;s sequence number. For example, this command replaces the first occurrence of the word <strong>&ldquo;music&rdquo; <\/strong>with <strong>&ldquo;song&rdquo; <\/strong>in a line inside the <strong>playlist.txt<\/strong> file:<\/p><pre class=\"wp-block-preformatted\">sed 's\/music\/song\/1' playlist.txt<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/sed-replaced-the-first-music-occurrence-with-song.png\"><img decoding=\"async\" width=\"800\" height=\"42\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/sed-replaced-the-first-music-occurrence-with-song.png\" alt=\"Terminal output shows sed has replaced the first music occurrence with song\" class=\"wp-image-94270\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-the-first-music-occurrence-with-song.png 800w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-the-first-music-occurrence-with-song-300x16.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-the-first-music-occurrence-with-song-150x8.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-the-first-music-occurrence-with-song-768x40.png 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\">Using sed to Replace All the Occurrences of the Pattern in a Line<\/h3><p>By default, the sed command replaces only the first instance of the specified string and moves to the next input line. To replace all matching patterns in the same line, add the <strong>g<\/strong> flag. Here&rsquo;s how the sed script looks:<\/p><pre class=\"wp-block-preformatted\">sed 's\/old_string\/new_string\/g' samplefile.txt<\/pre><p>For example, run the following to replace all occurrences that contain <strong>&ldquo;eagle&rdquo; <\/strong>with <strong>&ldquo;falcon&rdquo; <\/strong>in a line inside <strong>animals.txt<\/strong>:<\/p><pre class=\"wp-block-preformatted\">sed 's\/eagle\/falcon\/g' animals.txt<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/sed-replaced-all-eagle-occurrences-with-falcon.png\"><img decoding=\"async\" width=\"800\" height=\"43\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/sed-replaced-all-eagle-occurrences-with-falcon.png\" alt=\"Terminal output shows sed has replaced all eagle occurrences with falcon\" class=\"wp-image-94272\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-all-eagle-occurrences-with-falcon.png 800w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-all-eagle-occurrences-with-falcon-300x16.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-all-eagle-occurrences-with-falcon-150x8.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-all-eagle-occurrences-with-falcon-768x41.png 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\">Using sed to Replace an Occurrence From nth to All Occurrences in a Line<\/h3><p>Instead of replacing all patterns within the same line, combine the number and <strong>g <\/strong>flag to replace occurrences starting from a specific one. Here&rsquo;s the sed script:<\/p><pre class=\"wp-block-preformatted\">sed 's\/old_string\/new_string\/#g' samplefile.txt<\/pre><p>For example, the command below replaces the word <strong>&ldquo;pisces&rdquo; <\/strong>with <strong>&ldquo;aquarius&rdquo; <\/strong>from the second occurrence until the last one in the <strong>astrology.txt<\/strong> file.<\/p><pre class=\"wp-block-preformatted\">sed 's\/pisces\/aquarius\/2g' astrology.txt<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/sed-replaced-the-second-pisces-occurrence-until-the-last-one-with-aquarius.png\"><img decoding=\"async\" width=\"866\" height=\"42\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/sed-replaced-the-second-pisces-occurrence-until-the-last-one-with-aquarius.png\" alt=\"Terminal output shows sed has replaced the second pisces occurrence until the last one with aquarius\" class=\"wp-image-94274\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-the-second-pisces-occurrence-until-the-last-one-with-aquarius.png 866w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-the-second-pisces-occurrence-until-the-last-one-with-aquarius-300x15.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-the-second-pisces-occurrence-until-the-last-one-with-aquarius-150x7.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-the-second-pisces-occurrence-until-the-last-one-with-aquarius-768x37.png 768w\" sizes=\"(max-width: 866px) 100vw, 866px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\">Using sed to Parenthesize the First Character of Each Word<\/h3><p>To print the first character of every word in parenthesis, use the following script:<\/p><pre class=\"wp-block-preformatted\">echo \"desired_sentence\" | sed -E 's\/(\\b\\w)\/(\\1)\/g'<\/pre><p>For example, to display the first character of <strong>&ldquo;An example of the sed command&rdquo;<\/strong> in parenthesis, enter:<\/p><pre class=\"wp-block-preformatted\">echo \"An example of the sed command\" | sed -E 's\/(\\b\\w)\/(\\1)\/g'<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/sed-parenthesizes-each-word-first-character.png\"><img decoding=\"async\" width=\"959\" height=\"65\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/sed-parenthesizes-each-word-first-character.png\" alt=\"Sed parenthesizes each word's first character of the piped sentence\" class=\"wp-image-94275\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-parenthesizes-each-word-first-character.png 959w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-parenthesizes-each-word-first-character-300x20.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-parenthesizes-each-word-first-character-150x10.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-parenthesizes-each-word-first-character-768x52.png 768w\" sizes=\"(max-width: 959px) 100vw, 959px\" \/><\/a><\/figure><\/div><p>To parenthesize each word&rsquo;s first character from a file, omit the <strong>echo<\/strong> command and add the input document at the end.<\/p><h3 class=\"wp-block-heading\">Using sed to Replace the String on a Specific Line Number<\/h3><p>To replace the string on an nth line, add its sequence number before <strong>s<\/strong> like this syntax:<\/p><pre class=\"wp-block-preformatted\">sed '#s\/old_string\/new_string\/' samplefile.txt<\/pre><p>For example, enter the following to substitute the word <strong>&ldquo;cake&rdquo; <\/strong>with <strong>&ldquo;bread&rdquo; <\/strong>in the second line of <strong>foods.txt<\/strong>:<\/p><pre class=\"wp-block-preformatted\">sed '2s\/cake\/bread\/' foods.txt<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/sed-replaced-cake-in-the-second-line-with-bread.png\"><img decoding=\"async\" width=\"800\" height=\"85\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/sed-replaced-cake-in-the-second-line-with-bread.png\" alt=\"Terminal output shows sed has replaced cake in the second line with bread\" class=\"wp-image-94276\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-cake-in-the-second-line-with-bread.png 800w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-cake-in-the-second-line-with-bread-300x32.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-cake-in-the-second-line-with-bread-150x16.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-cake-in-the-second-line-with-bread-768x82.png 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\">Using sed to Duplicate the Replaced Line With the \/p Flag<\/h3><p>To print lines that your sed command modified as an additional output, use the <strong>p<\/strong> or <strong>print <\/strong>flag. Here&rsquo;s the general syntax:<\/p><pre class=\"wp-block-preformatted\">sed 's\/old_string\/new_string\/p' samplefile.txt<\/pre><p>For example, run the following to replace <strong>&ldquo;phones&rdquo; <\/strong>with <strong>&ldquo;tablets&rdquo; <\/strong>in the <strong>gadgets.txt<\/strong> file and print the results:<\/p><pre class=\"wp-block-preformatted\">sed 's\/phones\/tablets\/p' gadgets.txt<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-prints-replaced-line-as-an-output.png\"><img decoding=\"async\" width=\"800\" height=\"108\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-prints-replaced-line-as-an-output.png\" alt=\"Terminal prints the replaced line as an additional output\" class=\"wp-image-94277\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/terminal-prints-replaced-line-as-an-output.png 800w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/terminal-prints-replaced-line-as-an-output-300x41.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/terminal-prints-replaced-line-as-an-output-150x20.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/terminal-prints-replaced-line-as-an-output-768x104.png 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/figure><\/div><p>Terminal will print the original string if the line doesn&rsquo;t contain the search pattern and is not substituted.<\/p><h3 class=\"wp-block-heading\">Using sed to Replace the String of a Range of Lines<\/h3><p>The sed command lets you modify only the line numbers specified in the script by adding the range. Here&rsquo;s the syntax:<\/p><pre class=\"wp-block-preformatted\">sed '#,# s\/old_string\/new_string\/' samplefile.txt<\/pre><p>For example, the command below replaces <strong>&ldquo;germany&rdquo;<\/strong> located in the third, fourth, and fifth line on the <strong>countries.txt<\/strong> file with <strong>&ldquo;france&rdquo;<\/strong>:<\/p><pre class=\"wp-block-preformatted\">sed '3,5 s\/germany\/france\/' countries.txt<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/sed-replaced-the-germany-occurrence-on-the-third-to-fifth-line-with-france.png\"><img decoding=\"async\" width=\"825\" height=\"128\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/sed-replaced-the-germany-occurrence-on-the-third-to-fifth-line-with-france.png\" alt=\"Terminal output shows sed has replaced germany on the third to fifth line with france\" class=\"wp-image-94278\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-the-germany-occurrence-on-the-third-to-fifth-line-with-france.png 825w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-the-germany-occurrence-on-the-third-to-fifth-line-with-france-300x47.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-the-germany-occurrence-on-the-third-to-fifth-line-with-france-150x23.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-replaced-the-germany-occurrence-on-the-third-to-fifth-line-with-france-768x119.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\">Using sed to Print Only the Replaced Lines<\/h3><p>By default, the stream editor prints the entire file content. To simplify the output, combine the <strong>-n <\/strong>option with the <strong>p <\/strong>command to show only the matching lines. Here&rsquo;s the general syntax:<\/p><pre class=\"wp-block-preformatted\">sed -n 's\/old_string\/new_string\/p' samplefile.txt<\/pre><p>For example, to replace the third instance of <strong>&ldquo;green&rdquo; <\/strong>with <strong>&ldquo;blue<\/strong>&ldquo;<strong> <\/strong>in a line inside the <strong>colors.txt <\/strong>file and print the modified lines on the terminal window, enter:<\/p><pre class=\"wp-block-preformatted\">sed -n 's\/green\/blue\/3p' colors.txt<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-prints-a-single-line-that-sed-modified.png\"><img decoding=\"async\" width=\"800\" height=\"45\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/terminal-prints-a-single-line-that-sed-modified.png\" alt=\"Terminal prints a single line that sed modified\" class=\"wp-image-94279\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/terminal-prints-a-single-line-that-sed-modified.png 800w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/terminal-prints-a-single-line-that-sed-modified-300x17.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/terminal-prints-a-single-line-that-sed-modified-150x8.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/terminal-prints-a-single-line-that-sed-modified-768x43.png 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\">Using sed to Delete Lines From a Particular File<\/h3><p>The <strong>d <\/strong>or<strong> delete <\/strong>command lets you remove lines from a file without a text editor. For example, use the following syntax to remove a particular line number:<\/p><pre class=\"wp-block-preformatted\">sed '#d' samplefile.txt<\/pre><p>Replace the hash (#) symbol with the line number you want to delete. For example, run this command to remove the first line from the<strong> cities.txt<\/strong> file:<\/p><pre class=\"wp-block-preformatted\">sed '1d' cities.txt<\/pre><p>In addition, you can delete all the lines within a specific range using the sed command:<\/p><pre class=\"wp-block-preformatted\">sed '#,#d' samplefile.txt<\/pre><p>Replace the hash (#) symbols with the starting and ending line numbers. For example, enter the following to delete the first to the third line in the <strong>cars.txt<\/strong> file:<\/p><pre class=\"wp-block-preformatted\">sed '1,3d' cars.txt<\/pre><p>You can also delete the last line in a file by combining the <strong>d <\/strong>subcommand and a dollar sign ($), like the following.<\/p><pre class=\"wp-block-preformatted\">sed '$d' samplefile.txt<\/pre><p>To delete a specific line number starting from the last one, use the following syntax:<\/p><pre class=\"wp-block-preformatted\">sed '#,$d' samplefile.txt<\/pre><p>For example, this command will remove the second to last line in the <strong>books.txt<\/strong> file:<\/p><pre class=\"wp-block-preformatted\">sed '2,$d' books.txt<\/pre><p>In addition to deleting lines, use this command to remove a particular occurrence in a file. To do so, specify the regex pattern in your script, like the following syntax:<\/p><pre class=\"wp-block-preformatted\">sed '\/pattern\/d' samplefile.txt<\/pre><p>For example, run this to remove the <strong>&ldquo;oabo&rdquo; <\/strong>pattern from the <strong>filestrings.txt<\/strong> file:<\/p><pre class=\"wp-block-preformatted\">sed '\/oabo\/d' filestrings.txt<\/pre><h2 class=\"wp-block-heading\" id=\"h-sed-command-use-cases\">sed Command Use Cases<\/h2><p>In this section, we will explain how to use sed commands for different use cases in server management.<\/p><p><strong>Use sed for<\/strong> <strong>Batch Processing of Files<\/strong><\/p><p>Generally, there are two ways to edit files in bulk using the sed command.<\/p><p>First, specify the files individually. With this method, you will list all the input files you want to replace at the end of your command, separated using spaces. Here&rsquo;s the syntax:<\/p><pre class=\"wp-block-preformatted\">sed 's\/old_string\/new_string\/g' filename1.txt filename2.txt<\/pre><p>The command will simultaneously find and replace all <strong>old_string<\/strong> occurrences in the two text files.<\/p><p>Second, scan them using the <strong>find <\/strong>command. This method automatically searches for files containing the specified pattern in a directory. Here&rsquo;s the syntax:<\/p><pre class=\"wp-block-preformatted\">find \/directory\/path\/file -type f -exec sed -i 's\/old_string\/new_string\/g' {} \\;<\/pre><p>Replace <strong>\/directory\/path\/file <\/strong>with the directory containing the files you want to process.<\/p><p>When using the second method, create a backup file for easy restoration in case of accidental substitutions. Alternatively, omit the <strong>-i <\/strong>option to disable in-place editing and keep the original file unaltered.<\/p><p><strong>Use sed to<\/strong> <strong>Log File Analysis<\/strong><\/p><p>In addition to modifying patterns, the sed command in Linux is also useful for log analysis. It lets you easily search for a specific pattern in the log file and extract the results for easier diagnostics.<\/p><p>To do so, add the <strong>&gt; <\/strong>symbol to output matching patterns into a text<strong> <\/strong>file. While the command differs depending on your usage, the basic syntax remains as follows:<\/p><pre class=\"wp-block-preformatted\">sed -n 's\/pattern\/p' logfile.log &gt; extracted_data.txt<\/pre><p>Change the regex pattern according to the data you want to search for, like IP address, error message, or timestamp. For example, use this script to export logged errors:<\/p><pre class=\"wp-block-preformatted\">sed -n 's\/Error: \\(.*\\)\/\\1\/p' logfile.log &gt; error_logs.txt<\/pre><p><strong>Use sed for HTML\/XML Tag Manipulation<\/strong><\/p><p>The sed command in Linux lets you easily replace strings in code. For example, you can search for specific HTML or XML tags and replace their attributes using the <strong>substitution <\/strong>command. Here&rsquo;s the syntax:<\/p><pre class=\"wp-block-preformatted\">sed 's\/&lt;tag attribute=\"old_pattern\"&gt;\/&lt;tag attribute=\"new_pattern\"\/' file.html<\/pre><p>The command varies depending on the tags and attributes you want to change. For example, this sed command changes all heading tags&rsquo; <strong>color <\/strong>attributes to <strong>black<\/strong>:<\/p><pre class=\"wp-block-preformatted\">sed 's\/\\(&lt;h[1-6].*color:\\) [^;]*\/\\1 black\/g' webpage.html<\/pre><p>The command will search for the following HTML code pattern and replace the color value with the new string:<\/p><pre class=\"wp-block-preformatted\">&lt;h1 style=\"color: value;\"&gt;Heading 1&lt;\/h1&gt;<\/pre><p><strong>Use sed for External Files<\/strong><\/p><p>Complex sed command operations may contain multiple scripts. While you can add them in a single command with the <strong>-e<\/strong> option, it is difficult to write and prone to error.<\/p><p>Alternatively, create an external <strong>SED <\/strong>file containing multiple scripts. Here are the steps:<\/p><ol class=\"wp-block-list\">\n<li>Run the <strong>nano<\/strong> command to create a new script file and open the text editor. Replace <strong>script <\/strong>with your desired file name:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">nano script.sed<\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li>Write your scripts and ensure each line contains one script without apostrophes, like the following:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">s\/old_pattern1\/new_pattern1\/g<\/pre><pre class=\"wp-block-preformatted\">\/old_pattern2\/d<\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li>Press <strong>Ctrl+X<\/strong> and <strong>Y<\/strong> to close the editor and save the file.<\/li>\n\n\n\n<li>Run the scripts by adding the file using the <strong>-f <\/strong>option in your command. Here&rsquo;s what the syntax looks like:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sed -f script.sed destination_file.txt<\/pre><p><strong>Use sed for Backreferences in Regex<\/strong><\/p><p><a href=\"https:\/\/www.regular-expressions.info\/backref.html\" target=\"_blank\" rel=\"noreferrer noopener\">Backreferences<\/a> let you refer to previously matched patterns and reuse them in the new string. It eliminates the need to rewrite the regex pattern, simplifying the script.<\/p><p>The pattern used as the sed command reference is called a <strong>capture group<\/strong>. Encapsulated in parentheses, it also slashes for extended regular expressions.<\/p><p>To refer to the capture group, use a backslash (\\) and a number indicating the pattern order. For instance, the <strong>\\1<\/strong> backreference will reuse the first captured regex pattern.<\/p><p>Backreferences are useful for substituting and reordering patterns. For example, this sed command uses them to reorder last and first names:<\/p><pre class=\"wp-block-preformatted\">echo \"Doe, John\" | sed 's\/\\(.*\\), \\(.*\\)\/\\2 \\1\/'<\/pre><p>The two <strong>\\(.*\\)<\/strong> regex patterns capture Doe and John, setting them as the reference. Since the new string starts with the second backreference, the new value will be <strong>John Doe<\/strong>.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/sed-reorders-string-with-backreferences.png\"><img decoding=\"async\" width=\"906\" height=\"46\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/sed-reorders-string-with-backreferences.png\" alt=\"Sed reorders string with backreferences\" class=\"wp-image-94280\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-reorders-string-with-backreferences.png 906w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-reorders-string-with-backreferences-300x15.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-reorders-string-with-backreferences-150x8.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2019\/03\/sed-reorders-string-with-backreferences-768x39.png 768w\" sizes=\"(max-width: 906px) 100vw, 906px\" \/><\/a><\/figure><\/div><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>The stream editor or sed command in Linux is a tool that lets you find and modify a pattern in a file using regular expressions. Its syntax comprises the sed command, options, the script, and the target file.<\/p><p>sed command-line options modify the tool&rsquo;s behavior, while the script determines how the tool modifies the matching regex pattern. The script contains a subcommand, flags, and the regex patterns, each separated using a delimiter like a slash (\/).<\/p><p>To use the sed command in Linux, open command-line applications like Terminal, an SSH client, or Hostinger&rsquo;s Browser Terminal. To find and replace a pattern, use the <strong>s <\/strong>subcommand. Meanwhile, the <strong>d <\/strong>subcommand deletes lines or strings.<\/p><p>The sed command in Linux is also useful for log analysis, modifying markup tags, and processing files in bulk. Moreover, it supports regex backreferences and lets you use a script file to run multiple Linux commands simultaneously.<\/p><h2 class=\"wp-block-heading\" id=\"h-linux-sed-command-faq\">Linux sed Command FAQ<\/h2><p>This section will answer the most frequently asked questions about the sed command.<\/p><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1668095911341\"><h3 class=\"schema-faq-question\">How Is sed Different From grep?<\/h3> <p class=\"schema-faq-answer\">Both<strong> sed<\/strong> and <a href=\"\/uk\/tutorials\/grep-command-in-linux-useful-examples\/\">grep<\/a> are text processing tools in Linux. However, grep doesn&rsquo;t support text transformation or line manipulation commands like substitution. It is primarily used to find particular text patterns in large files and print the output.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1668095930629\"><h3 class=\"schema-faq-question\">How Is sed Used in the Bash Script?<\/h3> <p class=\"schema-faq-answer\">In the Bash script, the sed command has three primary uses &ndash; printing to stdout, deleting a text, and replacing a specific string. The program determines which line it will process from the specified address range.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1668095945086\"><h3 class=\"schema-faq-question\">How Do You Call a Variable in the sed Command?<\/h3> <p class=\"schema-faq-answer\">A variable is a character we assign a value to, providing better readability when added to a sed command. It can be a number, character, file name, or device. The shell expands variables. For example, if the string contains a slash (<strong>\/<\/strong>), use another delimiter, such as a pipe (<strong>|<\/strong>).<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1668095963594\"><h3 class=\"schema-faq-question\">How Is the sed Command Different From the awk Command?<\/h3> <p class=\"schema-faq-answer\">The sed command is for basic text modification, like replacing, deleting, and inserting a pattern into an input file. Meanwhile, <a href=\"\/uk\/tutorials\/awk-command\">awk<\/a> is used for complex tasks like data extraction, text manipulation, and mathematical calculations. It also supports programming statements like <strong>if\/else<\/strong> and <strong>do\/while<\/strong>.<\/p> <\/div> <\/div><p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Linux sed command lets you find, replace, insert, and delete lines in a file without opening it using a text editor. Suitable for piped input and various file formats, this Linux command also uses regular expression or regex, allowing you to search and manipulate complex patterns. With its various use cases, understanding how to [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/uk\/tutorials\/linux-sed-command\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":102,"featured_media":87036,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to Use Linux sed Command + Free Linux Cheat Sheet","rank_math_description":"The Linux sed command stands for stream editor and is used for file editing. Learn how to use the sed command through useful examples.","rank_math_focus_keyword":"sed command","footnotes":""},"categories":[22644,22640],"tags":[],"class_list":["post-15984","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-managing-monitoring-and-security","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/linux-sed-command","default":0},{"locale":"fr-FR","link":"https:\/\/www.hostinger.com\/fr\/tutoriels\/sed-linux","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/comando-sed-linux","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/sed-linux","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/linux-sed-command","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/linux-sed-command","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/linux-sed-command","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/que-son-hooks-wordpress-6","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/que-son-hooks-wordpress-6","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/comando-sed-linux","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/linux-sed-command","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/linux-sed-command","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/linux-sed-command","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/linux-sed-command","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/15984","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/users\/102"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/comments?post=15984"}],"version-history":[{"count":37,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/15984\/revisions"}],"predecessor-version":[{"id":126218,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/15984\/revisions\/126218"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/media\/87036"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/media?parent=15984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/categories?post=15984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/tags?post=15984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}