{"id":98536,"date":"2020-10-27T11:00:04","date_gmt":"2020-10-27T08:00:04","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-modify-text-using-regular-expressions-with-the-sed-stream-editor-cloudsavvy-it\/"},"modified":"2020-10-27T11:00:04","modified_gmt":"2020-10-27T08:00:04","slug":"how-to-modify-text-using-regular-expressions-with-the-sed-stream-editor-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-modify-text-using-regular-expressions-with-the-sed-stream-editor-cloudsavvy-it\/","title":{"rendered":"#How to Modify Text Using Regular Expressions With the sed Stream Editor \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a26e953c73ed\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a26e953c73ed\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-modify-text-using-regular-expressions-with-the-sed-stream-editor-cloudsavvy-it\/#Regular_Expressions_At_The_Command_Line\" >Regular Expressions At The Command Line<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-modify-text-using-regular-expressions-with-the-sed-stream-editor-cloudsavvy-it\/#Using_sed_With_Files\" >Using sed With Files<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Modify Text Using Regular Expressions With the sed Stream Editor \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-7563\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/a02559a9accd6ed15163a044bf34bfc5\/p\/uploads\/2020\/10\/c20b8a4d.png\" alt=\"How to Modify Text Using Regular Expressions With the sed Stream Editor\" width=\"1200\" height=\"494\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Every computer user in the world sooner or later will modify a text string \u2013 either manually or automatically. Be introduced to the <strong>sed stream editor<\/strong>, which enables you to make bulk text modifications to strings and files.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Regular_Expressions_At_The_Command_Line\"><\/span>Regular Expressions At The Command Line<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The sed stream editor, a tool available by default on many Linux distributions, enables you to parse and transform text in an easy and straightforward manner, whether such text is inside files, or just plain strings at the command line. You can also parse directory listings, process listing output, and most other type of information which can flow in or out of your terminal session.<\/p>\n<p>To verify if sed is available on your Linux distribution, type <code>sed --version<\/code> at the command line:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-7564\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/171d95c6b04501f4c20d81792c47e33d\/p\/uploads\/2020\/10\/3c6449f3.png\" alt=\"sed stream Editor Version\" width=\"728\" height=\"250\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Do not worry if your version is slightly older then the one shown here. It will almost definitely fine for the examples we discuss here. If the tool is not available on your distribution, you can install it using <code>sudo apt install sed<\/code> for Ubuntu\/Debian based operating systems, or <code>sudo yum install sed<\/code> for RedHat\/RPM based operating systems.<\/p>\n<p><code>sed<\/code>\u00a0<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lies small text transformation using what is called a RegEx, or Regular Expression. A regular expression is a form and method of expressing more complex text-based search, replace and modify operations in a still human-readable format. Regular expressions are complicated, and may be hard to read for beginners. You can read our guide, How Do You Actually Use Regex, for more information on how to read and write Regex.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How Do You Actually Use Regex?<\/em><\/strong><\/p>\n<p>For the purpose of this article, we will stick with easy to understand and read regular expressions. For example, the following is a simple replacement:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7565\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/ace294cae6a7a34f683796363c99a0f6\/p\/uploads\/2020\/10\/ea5cc514.png\" alt=\"Hello Cloudsavvyit Fans Transformation\" width=\"734\" height=\"75\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>As you can see, we echo\u2019d a small piece of text, and subsequently transformed it using the sed tool. We employed the use of the \u2018s\u2019 (substitute) sed command and indicated what we wanted to change. You may read this in another way as <code>s|from|to|<\/code> where \u2018|\u2019 is the separator between sections\/fields, \u2018s\u2019 is the command and \u2018from\u2019 and \u2018to\u2019 are the two options passed to the \u2018s\u2019 command.<\/p>\n<p>We thus transformed \u2018Hello Cloud Savvy IT fans!\u2019 to \u2018Hello cloudsavvyit.com fans!\u2019 by only changing a part of the text using the sed stream editor. Let\u2019s take a slightly more complex example.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7566\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/175bbe46c2df7a7afe57cfa73a13ed15\/p\/uploads\/2020\/10\/a3356f1d.png\" alt=\"Transforming text in a file using the sed stream editor\" width=\"506\" height=\"249\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Here we first create a file with 3 lines using the <code>echo<\/code> command with redirection to a file (\u2018&gt;\u2019 creates a new file, overwriting a file with the same name if it exists, and \u2018&gt;&gt;\u2019 appends to a file without otherwise modifying it). Next we displayed the file contents using the <code>cat<\/code> command.<\/p>\n<p>We then transformed the contents of the file with the <code>sed<\/code> tool directly. To do so, we specified the \u2018-i\u2019 option to sed which edits file in-place. The actual textual transformation instruction is the same as before. And finally we specify the filename at the end of the command. We then check the file contents and notice how our input has changed.<\/p>\n<p>As you can start to see, or already know from past experience, there is a lot of power available in the sed tool.<\/p>\n<p>Let us spice things up a little by expanding on our regular expression and method of writing the sed text transformation instruction:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7567\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/d2fb75a22a931bc973196fcc4dded977\/p\/uploads\/2020\/10\/9eb83447.png\" alt=\"A more complex Regular Expression and sed instruction\" width=\"357\" height=\"197\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>In this example, we substituted the letter \u2018h\u2019 for \u2018H_\u2019 and we did so in a global (note the \u2018g\u2019 qualifier near the end of the first instruction passed to sed) and case insensitive manner (note the \u2018i\u2019 qualifier also near the end of the first instruction). The \u2018h\u2019 is matched even though the file contains \u2018H\u2019 due to the \u2018i\u2019 qualifier.<\/p>\n<p>We then started a new instruction (instructions are terminated\/ended by \u2018;\u2019 alike to many other coding languages) in which we deleted \u2018.com\u2019 by basically search for it and replacing it with nothing.<\/p>\n<p>Note also how we have used different field\/command option separators. In the first example we used \u2018|\u2019 which is overall an easy-to-use, and fairly uncommon field separator. Many people use \u2018\/\u2019 though this may lead to issues when you are parsing path names which often contain \u2018\/\u2019.<\/p>\n<p>Being able to use different field\/command option separators enables you to easily use another separator if your text string does actually or will likely contain a specific character.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Using_sed_With_Files\"><\/span>Using sed With Files<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Let\u2019s explore another sed based example where we will inject text into a file:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7568\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/bc4f11976982d69adbaf40bc7df58caa\/p\/uploads\/2020\/10\/aae4bd64.png\" alt=\"Injecting text using the sed stream editor\" width=\"406\" height=\"396\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>In this example, we created a file with 5 lines and subsequently used a slightly complex sed instruction to inject a \u2018\u2014\u2018 separator after every second line. While the syntax mostly explains itself, it is interesting to note that the \u2018r\u2019 (read\/append text from a file) command specifies the inject filename directly and while still inside the main instruction, without any separator.<\/p>\n<p>You can also start to see that, on top of learning a regular expression syntax, it is beneficial to learn sed syntax in depth. It is almost like learning two new foreign languages, but the benefit is that sed syntax can grow with your need for additional complexity.<\/p>\n<p>Let\u2019s explore a example where we replace a specific text area with something else:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7569\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/69c5f99c1117ed9825ba5d2dbed69f4c\/p\/uploads\/2020\/10\/8d27b915.png\" alt=\"Modifying specific text areas using sed\" width=\"264\" height=\"180\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>In this example, we search for the \u20183\u2019 text, and for that line we will substitute \u2018C\u2019 to \u2018Z\u2019. You see how versatile sed can be.<\/p>\n<p>Finally, we can look at parsing other types of input:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7570\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/17d7286300fb601ae687bffe4d42a7d6\/p\/uploads\/2020\/10\/ec0f091b.png\" alt=\"Parsing other types of textual input with sed\" width=\"190\" height=\"191\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>As you can see, we created 3 files, and requested a directory listing using the <code>ls<\/code> command. We then took that output as input to sed (using the Linux pipe command, \u2018|\u2019) and transformed part of our directory listing to an underscore.<\/p>\n<p>The sed stream editor is a versatile tool which can help you parse and transform almost any text directly from the terminal command line, whether the input comes from a simple <code>echo<\/code> statement, other Linux tools, or from a flat text file. <strong>Enjoy!<\/strong>\n<\/div>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener noreferrer\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/7561\/how-to-modify-text-using-regular-expressions-with-the-sed-stream-editor\/\" target=\"_blank\" rel=\"noopener noreferrer\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Modify Text Using Regular Expressions With the sed Stream Editor \u2013 CloudSavvy IT&#8221; Every computer user in the world sooner or later will modify a text string \u2013 either manually or automatically. Be introduced to the sed stream editor, which enables you to make bulk text modifications to strings and files. Regular Expressions&#8230;<\/p>\n","protected":false},"author":1,"featured_media":98537,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/10\/c20b8a4d.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-98536","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/98536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=98536"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/98536\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/98537"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=98536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=98536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=98536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}