{"id":3245,"date":"2025-02-19T20:32:17","date_gmt":"2025-02-19T20:32:17","guid":{"rendered":"https:\/\/foghosting.com\/linux-sed-command-from-basics-to-advanced-usage\/"},"modified":"2025-02-19T20:32:17","modified_gmt":"2025-02-19T20:32:17","slug":"linux-sed-command-from-basics-to-advanced-usage","status":"publish","type":"post","link":"https:\/\/foghosting.com\/blog\/linux-sed-command-from-basics-to-advanced-usage\/","title":{"rendered":"Linux sed Command: From Basics to Advanced Usage"},"content":{"rendered":"<div>\n<h2><span id=\"Introduction\"><strong>Introduction<\/strong><\/span><\/h2>\n<p>The <strong>Linux sed command<\/strong>, which stands for stream editor, edits text that comes from a file as well as standard input. The non-interactive editing tool sed performs line-by-line changes.<\/p>\n<p>This implies that you specify each of the editing choices while you call the command, as sed automatically carries out the instructions. Although this can be difficult to understand or appear unconventional, this method of text transformation is very effective and quick, particularly when used as a component of a script and automated processes.<\/p>\n<p>You will find out the syntax needed to use this editor in this guide, which also covers several fundamental actions. Sed won\u2019t likely ever be a substitute for your primary text editor, however, it\u2019ll probably end up being a useful tool in your toolkit. Using simple-to-follow instances discover how to navigate the <strong>Linux sed command <\/strong>along with its various options.<\/p>\n<h2><span id=\"Linux_sed_Syntax\"><strong>Linux sed Syntax<\/strong><\/span><\/h2>\n<p>The primary syntax associated with the <strong>sed command in Linux<\/strong> is as follows:<\/p>\n<pre><code><strong><em>sed OPTIONS... [SCRIPT] [INPUTFILE...]<\/em><\/strong><\/code><\/pre>\n<h3><span id=\"sed_Linux_Options\"><strong>sed Linux Options<\/strong><\/span><\/h3>\n<p>These command-line options are available for sed to run with:<\/p>\n<figure>\n<table readabilityDataTable=\"1\">\n<tbody>\n<tr>\n<td>-b, \u2013binary\u00a0<\/td>\n<td>Access input files using binary mode to treat line feeds as the end of a line.<\/td>\n<\/tr>\n<tr>\n<td>\u2013debug\u00a0<\/td>\n<td>Use the debug mode for printing input in its correct format and comment on how the program is running.<\/td>\n<\/tr>\n<tr>\n<td>\u2013follow-symlinks\u00a0<\/td>\n<td>In case the supplied file represents a symbolic link, modify the final destination. The -i option get required for it to function.<\/td>\n<\/tr>\n<tr>\n<td>\u2013help\u00a0<\/td>\n<td>Specify the usage details.<\/td>\n<\/tr>\n<tr>\n<td>\u2013i, \u2013in-place [=SUFFIX]\u00a0<\/td>\n<td>By overwriting the initially created file, make edits in place.<\/td>\n<\/tr>\n<tr>\n<td>\u2013posix\u00a0<\/td>\n<td>For easier portability of scripts, disable all POSIX sed extensions.<\/td>\n<\/tr>\n<tr>\n<td>\u2013version\u00a0<\/td>\n<td>Give a list of the sed versions active on the system.<\/td>\n<\/tr>\n<tr>\n<td>-E, -r, \u2013regexp-extended\u00a0<\/td>\n<td>Make use of expanded regular expressions.<\/td>\n<\/tr>\n<tr>\n<td>-e script, \u2013expression=script\u00a0<\/td>\n<td>To run alongside the commands, add a specific script.<\/td>\n<\/tr>\n<tr>\n<td>-f script-file\u00a0<\/td>\n<td>Specify a script-file along with adding its contents to the command line.<\/td>\n<\/tr>\n<tr>\n<td>-l N, \u2013line-length=N\u00a0<\/td>\n<td>The default value associated with the l command\u2019s line-wrap length is 70, therefore you have to provide a different length.<\/td>\n<\/tr>\n<tr>\n<td>-n, \u2013quiet, \u2013silent\u00a0<\/td>\n<td>Deactivate printing from the output.<\/td>\n<\/tr>\n<tr>\n<td>-s, \u2013separate\u00a0<\/td>\n<td>View the selected files individually rather than as one long stream of continuous data.<\/td>\n<\/tr>\n<tr>\n<td>\u2013sandbox\u00a0<\/td>\n<td>Disable running external programs and use the command line alone to work with input files.<\/td>\n<\/tr>\n<tr>\n<td>-u, \u2013unbuffered\u00a0<\/td>\n<td>Cut back on the input and output buffers.<\/td>\n<\/tr>\n<tr>\n<td>-z, \u2013null-data, \u2013zero-terminated\u00a0\u00a0<\/td>\n<td>View the input being a collection of lines, each of which terminates with a zero byte.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h2><span id=\"Linux_sed_Examples\"><strong>Linux sed Examples<\/strong><\/span><\/h2>\n<p>The ten most popular<strong> sed commands in Linux<\/strong>, examples, as well as syntax get listed below.<\/p>\n<p>The following text got typed into an example file called foxinbox.txt to illustrate this guide:<\/p>\n<ul>\n<li>Knox in box.<\/li>\n<li>Fox in socks.<\/li>\n<\/ul>\n<ul>\n<li>Knox on fox in socks in box.<\/li>\n<\/ul>\n<ul>\n<li>Socks on Knox and Knox in box.<\/li>\n<li>Fox in socks on box on Knox.<\/li>\n<\/ul>\n<p>Remember that unless told, sed doesn\u2019t change the source file. On the other hand, the command\u2019s results reveal the modifications done by the supplied command.<\/p>\n<p>When saving the changes, utilize the -i option to overwrite the initial file. Before verifying the command output, it is not advised to engage in such a practice.<\/p>\n<p>As an alternative, save the changes to a unique (or new) file. Add > newfilename.txt to the very last line of the command to reroute the output.<\/p>\n<p>Also Read: Learn to use SCP Command in Linux<\/p>\n<h2><span id=\"Replace_String_Using_the_sed_Command\"><strong>Replace String Using the sed Command<\/strong><\/span><\/h2>\n<p>Most often, text replacements get done using the <strong>Linux sed Command<\/strong>. It looks for the supplied structure in a file and then changes it with the requested string.<\/p>\n<p>Apply the delimiters (often slashes \u2013 \/) to separate text fields alongside the substitute commands for replacing text using sed.<\/p>\n<p>In light of this, the syntax to use for substituting text is:<\/p>\n<pre><code><strong><em>sed 's\/old_string\/new_string\/' filename.txt<\/em><\/strong><\/code><\/pre>\n<p>Both old_string as well as new_string should be changed to reflect the new text you intend to use in its place.<\/p>\n<p>Perform the following command to substitute the word bin for scenarios of the word box:<\/p>\n<pre><code><strong><em>sed 's\/box\/bin\/' foxinbox.txt<\/em><\/strong><\/code><\/pre>\n<figure><\/figure>\n<p>The influenced words are seen in the output together with the original content.<\/p>\n<h2><span id=\"Use_the_sed_Command_to_Replace_any_Possible_Instances_of_a_String\"><strong>Use the sed Command to Replace any Possible Instances of a String<\/strong><\/span><\/h2>\n<p>The first instance of the provided string in each line is all that gets replaced by default when using sed. It looks for the very first occurrence of the chosen term in a line, changes it, and afterward proceeds on to the subsequent line.<\/p>\n<p>When there are multiple occurrences of the term box in a single line, the command merely replaces the first one.<\/p>\n<p>Add the g flag to the command to modify all occurrences of the same term when it appears more than once in a single line.<\/p>\n<p>The following command can be used to replace a specific string everywhere it appears in a text:<\/p>\n<pre><code><strong><em>sed 's\/old_string\/new_string\/g' filename.txt<\/em><\/strong><\/code><\/pre>\n<p>If you want to consistently substitute the term box with the phrase bin in the file foxinbox.txt, format:<\/p>\n<pre><code><strong><em>sed 's\/box\/bin\/g' foxinbox.txt<\/em><\/strong><\/code><\/pre>\n<p>The command substituted every single instance of the term box, as evidenced by the result shown above.<\/p>\n<h2><span id=\"Replace_Specific_Occurrence_in_a_Line_Using_the_sed_Command\"><strong>Replace Specific Occurrence in a Line Using the sed Command<\/strong><\/span><\/h2>\n<p>Selecting which instance of a given string you would like to get substituted inside each line is possible using the sed program. Add a flag with a number, like 1, 2, etc., to accomplish this:<\/p>\n<pre><code><strong><em>sed 's\/old_string\/new_string\/#' filename.txt<\/em><\/strong><\/code><\/pre>\n<p>Employ the following command, for instance, to replace the word bin after the word box on every single line of the text:<\/p>\n<pre><code><strong><em>sed 's\/box\/bin\/2' foxinbox.txt<\/em><\/strong><\/code><\/pre>\n<figure><\/figure>\n<p>Due to their order in the command, each of the initial three occurrences of the term got skipped, while only the final one got replaced.<\/p>\n<h2><span id=\"Only_Print_Lines_With_Substitute_Text\"><strong>Only Print Lines With Substitute Text<\/strong><\/span><\/h2>\n<p>The output of the <strong>sed command on Linux<\/strong> by standard includes both the original text and the substituted text. Add the necessary properties to the command when you\u2019re working with a great deal of text and wish to highlight the lines where the modifications got made.<\/p>\n<p>Employ the following syntax to print only the lines that, in the circumstances specified, have substitutions:<\/p>\n<pre><code><strong><em>sed -n 's\/old_string\/new_string\/p' filename.txt<\/em><\/strong><\/code><\/pre>\n<p>In contrast to the substitute command p, which tells sed to print lines wherever replacement happens, the -n option stops automated printing.<\/p>\n<p>Like in the following example, you can supplement other commands with the p command:<\/p>\n<pre><code><strong><em>sed -n 's\/box\/bin\/2p' foxinbox.txt<\/em><\/strong><\/code><\/pre>\n<figure><\/figure>\n<p>The command substitutes the second occurrence of the word box within a line and outputs the line that got changed.\u00a0<\/p>\n<h2><span id=\"Replace_String_Using_the_sed_Command_and_Ignore_Case\"><strong>Replace String Using the sed Command and Ignore Case<\/strong><\/span><\/h2>\n<p>By using <strong>Linux sed command<\/strong>, the design, takes the case into account. Like the word \u201cfox,\u201d which has both upper- and lowercase initial letters in the text below:<\/p>\n<ul>\n<li>Knox in box.<\/li>\n<li>Fox in socks.<\/li>\n<\/ul>\n<ul>\n<li>Knox on fox in socks in box.<\/li>\n<\/ul>\n<ul>\n<li>Socks on Knox and Knox in box.<\/li>\n<li>Fox in socks on box on Knox.<\/li>\n<\/ul>\n<p>Perform the following command instead to replace the word \u201cfox\u201d across all situations.<\/p>\n<pre><code><strong><em>sed 's\/fox\/cow\/' foxinbox.txt<\/em><\/strong><\/code><\/pre>\n<p>When the word \u201cfox\u201d appears entirely in lowercase, the command\u2019s outcome only affects those instances:<\/p>\n<p>When replacing text, <strong>add i sed command<\/strong> at the final part of the command to disregard the case:<\/p>\n<pre><code><strong><em>sed 's\/old_string\/new_string\/i' filename.txt<\/em><\/strong><\/code><\/pre>\n<p>Considering the text above, the command to change the word \u201cfox\u201d between upper- and lowercase is thus:<\/p>\n<pre><code><strong><em>sed 's\/fox\/cow\/i' foxinbox.txt<\/em><\/strong><\/code><\/pre>\n<figure><\/figure>\n<p>As can get seen in the output, the command modified all three cases of the provided string.<\/p>\n<h2><span id=\"Replace_String_in_Specific_Line_Using_the_sed_Command\"><strong>Replace String in Specific Line Using the sed Command<\/strong><\/span><\/h2>\n<p>By introducing the line number as a prefix in the s subcommand, the <strong>Linux<\/strong>\u00a0 <strong>sed command<\/strong> enables you to replace a string in a particular line:<\/p>\n<pre><code><strong><em>sed '# s\/old_string\/new_string\/' filename.txt<\/em><\/strong><\/code><\/pre>\n<p>Use the following command to switch the word socks just on the fourth line of the example foxinbox.txt file to the word sandals to evaluate this functionality:<\/p>\n<pre><code><strong><em>sed '4 s\/socks\/sandals\/' foxinbox.txt<\/em><\/strong><\/code><\/pre>\n<figure><\/figure>\n<p>The result reveals that even though the word \u201csocks\u201d has got changed to \u201csandals\u201d in the fourth line, it still appears in the first instance.<\/p>\n<h2><span id=\"Replace_String_Only_in_Specific_Range_of_Lines_Using_the_sed_Command\"><strong>Replace String Only in Specific Range of Lines Using the sed Command<\/strong><\/span><\/h2>\n<p>When you would like sed to substitute several occurrences of a string inside a line range rather than the whole thing, you have to give the range. As for the syntax:<\/p>\n<pre><code><strong><em>sed '#,# s\/old_string\/new_string\/' filename.txt<\/em><\/strong><\/code><\/pre>\n<p>By changing the primary # to the start line number plus the next # to the latest line number you wish to include, you can add more lines.<\/p>\n<p>As an example, to substitute the word sandals for the final two occurrences of the word socks in the file foxinbox.txt (found in the fourth and sixth lines), carry out:<\/p>\n<pre><code><strong><em>sed '4,6 s\/socks\/sandals\/' foxinbox.txt<\/em><\/strong><\/code><\/pre>\n<figure><\/figure>\n<h2><span id=\"Delete_Specific_Line_Using_the_sed_Command\"><strong>Delete Specific Line Using the sed Command<\/strong><\/span><\/h2>\n<p>Through the <strong>Linux sed command<\/strong>, employ the d subcommand as well as the following syntax to remove a line from a file:<\/p>\n<pre><code><strong><em>sed '#d' filename.txt<\/em><\/strong><\/code><\/pre>\n<p>Execute the command and substitute the hash (#) symbol for the line number you would like to remove.<\/p>\n<p>Using the foxinbox.txt file as an example, type: to delete the second line.<\/p>\n<pre><code><em><strong>sed '2d' foxinbox.txt<\/strong><\/em><\/code><\/pre>\n<figure><\/figure>\n<h2><span id=\"Delete_Lines_Within_a_Specific_Range_of_Lines_Using_the_sed_Command\"><strong>Delete Lines Within a Specific Range of Lines Using the sed Command<\/strong><\/span><\/h2>\n<p>The <strong>sed command\u2019s syntax<\/strong> is as follows to remove several lines from a line range:<\/p>\n<pre><code><strong><em>sed '#,#d' filename.txt<\/em><\/strong><\/code><\/pre>\n<p>The start and finish of the line range should be used as a substitute for the hash symbols. For example, these:<\/p>\n<pre><code><strong><em>sed '2,4d' foxinbox.txt<\/em><\/strong><\/code><\/pre>\n<figure><\/figure>\n<p>This command clears outlines two, three, and four from the file.<\/p>\n<h2><span id=\"Delete_From_Specific_to_Last_Line_Using_the_sed_Command\"><strong>Delete From Specific to Last Line Using the sed Command<\/strong><\/span><\/h2>\n<p>Utilize the following command to remove all lines between a particular line number as well as the last line:<\/p>\n<pre><code><strong><em>sed '#,$d' filename.txt<\/em><\/strong><\/code><\/pre>\n<p>This command should be used to delete anything in foxinbox.txt starting at line\u00a0<\/p>\n<pre><code><strong><em>sed '3,$d' foxinbox.txt<\/em><\/strong><\/code><\/pre>\n<figure><\/figure>\n<p>Also Read: 14 Tar Command in Linux<\/p>\n<h2><span id=\"Conclusion\"><strong>Conclusion<\/strong><\/span><\/h2>\n<p>This guide demonstrated the method to edit a file using the <strong>Linux sed command <\/strong>line using the text stream editor SED. Decide whether to keep the old file after you have updated the text and saved the latest version of it. And <strong>how to use sed command in Linux<\/strong>, you will be prepared to discover how you can quickly change a text file.<\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Introduction The Linux sed command, which stands for stream editor, edits text that comes from a file as well as standard input. The non-interactive editing tool sed performs line-by-line changes. This implies that you specify each of the editing choices while you call the command, as sed automatically carries out the instructions. Although this can<\/p>\n","protected":false},"author":3,"featured_media":3076,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[17],"tags":[],"class_list":["post-3245","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dedicated-servers"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/posts\/3245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/comments?post=3245"}],"version-history":[{"count":0,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/posts\/3245\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/media\/3076"}],"wp:attachment":[{"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/media?parent=3245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/categories?post=3245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/tags?post=3245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}