{"id":2166,"date":"2016-07-05T16:58:21","date_gmt":"2016-07-05T20:58:21","guid":{"rendered":"https:\/\/www.globo.tech\/learning-center\/?p=2166"},"modified":"2017-12-12T15:44:46","modified_gmt":"2017-12-12T20:44:46","slug":"use-command-sed-find-replace","status":"publish","type":"post","link":"https:\/\/www.globo.tech\/learning-center\/use-command-sed-find-replace\/","title":{"rendered":"How to use the command SED to find and replace"},"content":{"rendered":"<h1>How to use the command SED to find and replace<\/h1>\n<p>The command &#8220;sed&#8221; is a powerful Unix utility and stream editor that enables users to parse and transform text. Despite being available since 1974, oftentimes people remain unaware of how to best utilize its powers in order to streamline tasks or challenges they are working on. <\/p>\n<p>Sed works by reading files, line by line, into an internal buffer also known as the pattern space. Within this pattern space, sed applies a command that is given to it via the sed script provided by the user. This script entails one or more operations that encapsulate what the user desires to happen within the file. After sed is finished reading all lines of the file and applying modifications, it closes and the original file now contains the desired transformations<\/p>\n<p>Mastery of sed particularly has a lot of possible implications for the automation of tasks, mass file processing, or when working with templates, as you will see below. Essentially, what sed allows is the finding of a special string in a file and replacement with another string, all without opening the file itself. You can see why this makes it a perfect command for automation! With this one simple example, we show you exactly how you can start using sed for yourself today:<\/p>\n<h2>Getting Started<\/h2>\n<p>To complete this guide, you will need the following:<br \/>\n\u2022 1 Node (<a href=\"https:\/\/www.globo.tech\/cloud-server-pricing\" target=\"_blank\">Cloud Server<\/a> or <a href=\"http:\/\/www.globo.tech\/dedicated-server-hosting\" target=\"_blank\">Dedicated Server<\/a>) with any Linux distribution installed. <\/p>\n<h2>Using SED<\/h2>\n<p>First, we need to create a text file with some standard content to later modify with sed. In your chosen directory, run the command below:<\/p>\n<p><code>echo Good morning, what a nice morning today. > file.txt<\/code><\/p>\n<p><code class=\"gris\">cat file.txt<br \/>\nGood morning, what a nice morning today.<\/code><\/p>\n<p>Now that we have our text file, we can change the word &#8220;morning&#8221; to &#8220;afternoon&#8221; using sed on the command line. There are several modifications you can make when running sed that allow you to specify exactly how you want to replace your chosen expression. In our command below, we use the -i option to edit our file in place, so without opening it, and the -e option to indicate that this is the sed script we want to run on our file. The expression itself contains s\/ to represent which words are being replaced by what, and \/g to indicate replacing our expression globally, so not only the first instance of &#8220;morning&#8221; will be modified in case it shows up more than once on the same line.<\/p>\n<p><code>sed -i -e 's\/morning\/afternoon\/g' file.txt<\/code><\/p>\n<p><code class=\"gris\">cat file.txt<br \/>\nGood afternoon, what a nice afternoon today.<\/code><\/p>\n<p>This changes the file. We can now double check the new content of the updated text file:<\/p>\n<p><code>cat file.txt<\/code><\/p>\n<p>You will see how &#8220;morning&#8221; has now been changed to &#8220;afternoon.&#8221; That&#8217;s it, all you need to start using sed immediately!<\/p>\n<h2>Conclusion<\/h2>\n<p>You are now ready to use sed to replace file content on the fly. sed is a powerful utility with a huge number of possible applications and there are far many more existing options you can use with sed in order to really feel its power. Even by using sed to replace particular expressions or patterns within files, without opening the files themselves, as shown in this tutorial, you can save yourself a lot of time and effort in an endless variety of tasks. If this guide was helpful to you, kindly share it with others who may also be interested. <\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>How to use the command SED to find and replace The command &#8220;sed&#8221; is a powerful Unix utility and stream editor that enables users to parse and transform text. Despite being available since 1974, oftentimes people remain unaware of how to best utilize its powers in order to streamline tasks or challenges they are working<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[],"class_list":["post-2166","post","type-post","status-publish","format-standard","hentry","category-web-hosting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to use the command SED to find and replace - Globo.Tech<\/title>\n<meta name=\"description\" content=\"This tutorial will show you how to use the command SED to find and replace content on your server. Read now &amp; start parsing and transforming text!\" \/>\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.globo.tech\/learning-center\/use-command-sed-find-replace\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use the command SED to find and replace - Globo.Tech\" \/>\n<meta property=\"og:description\" content=\"This tutorial will show you how to use the command SED to find and replace content on your server. Read now &amp; start parsing and transforming text!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.globo.tech\/learning-center\/use-command-sed-find-replace\/\" \/>\n<meta property=\"og:site_name\" content=\"Globo.Tech\" \/>\n<meta property=\"article:published_time\" content=\"2016-07-05T20:58:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-12-12T20:44:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.globo.tech\/learning-center\/wp-content\/uploads\/2016\/09\/GloboTech-Logo.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"963\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"GloboTech Communications\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"GloboTech Communications\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.globo.tech\/learning-center\/use-command-sed-find-replace\/\",\"url\":\"https:\/\/www.globo.tech\/learning-center\/use-command-sed-find-replace\/\",\"name\":\"How to use the command SED to find and replace - Globo.Tech\",\"isPartOf\":{\"@id\":\"https:\/\/www.globo.tech\/learning-center\/#website\"},\"datePublished\":\"2016-07-05T20:58:21+00:00\",\"dateModified\":\"2017-12-12T20:44:46+00:00\",\"author\":{\"@id\":\"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87\"},\"description\":\"This tutorial will show you how to use the command SED to find and replace content on your server. Read now & start parsing and transforming text!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.globo.tech\/learning-center\/use-command-sed-find-replace\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.globo.tech\/learning-center\/use-command-sed-find-replace\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.globo.tech\/learning-center\/use-command-sed-find-replace\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.globo.tech\/learning-center\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to use the command SED to find and replace\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.globo.tech\/learning-center\/#website\",\"url\":\"https:\/\/www.globo.tech\/learning-center\/\",\"name\":\"Globo.Tech\",\"description\":\"Welcome to the Official Globo.Tech Learning Center\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.globo.tech\/learning-center\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87\",\"name\":\"GloboTech Communications\",\"sameAs\":[\"http:\/\/www.gtcomm.net\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to use the command SED to find and replace - Globo.Tech","description":"This tutorial will show you how to use the command SED to find and replace content on your server. Read now & start parsing and transforming text!","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.globo.tech\/learning-center\/use-command-sed-find-replace\/","og_locale":"en_US","og_type":"article","og_title":"How to use the command SED to find and replace - Globo.Tech","og_description":"This tutorial will show you how to use the command SED to find and replace content on your server. Read now & start parsing and transforming text!","og_url":"https:\/\/www.globo.tech\/learning-center\/use-command-sed-find-replace\/","og_site_name":"Globo.Tech","article_published_time":"2016-07-05T20:58:21+00:00","article_modified_time":"2017-12-12T20:44:46+00:00","og_image":[{"width":1920,"height":963,"url":"https:\/\/www.globo.tech\/learning-center\/wp-content\/uploads\/2016\/09\/GloboTech-Logo.png","type":"image\/png"}],"author":"GloboTech Communications","twitter_misc":{"Written by":"GloboTech Communications","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.globo.tech\/learning-center\/use-command-sed-find-replace\/","url":"https:\/\/www.globo.tech\/learning-center\/use-command-sed-find-replace\/","name":"How to use the command SED to find and replace - Globo.Tech","isPartOf":{"@id":"https:\/\/www.globo.tech\/learning-center\/#website"},"datePublished":"2016-07-05T20:58:21+00:00","dateModified":"2017-12-12T20:44:46+00:00","author":{"@id":"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87"},"description":"This tutorial will show you how to use the command SED to find and replace content on your server. Read now & start parsing and transforming text!","breadcrumb":{"@id":"https:\/\/www.globo.tech\/learning-center\/use-command-sed-find-replace\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.globo.tech\/learning-center\/use-command-sed-find-replace\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.globo.tech\/learning-center\/use-command-sed-find-replace\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.globo.tech\/learning-center\/"},{"@type":"ListItem","position":2,"name":"How to use the command SED to find and replace"}]},{"@type":"WebSite","@id":"https:\/\/www.globo.tech\/learning-center\/#website","url":"https:\/\/www.globo.tech\/learning-center\/","name":"Globo.Tech","description":"Welcome to the Official Globo.Tech Learning Center","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.globo.tech\/learning-center\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87","name":"GloboTech Communications","sameAs":["http:\/\/www.gtcomm.net"]}]}},"_links":{"self":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts\/2166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/comments?post=2166"}],"version-history":[{"count":5,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts\/2166\/revisions"}],"predecessor-version":[{"id":3985,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts\/2166\/revisions\/3985"}],"wp:attachment":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/media?parent=2166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/categories?post=2166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/tags?post=2166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}