{"id":425,"date":"2009-10-07T06:04:57","date_gmt":"2009-10-07T06:04:57","guid":{"rendered":"http:\/\/g33kinfo.com\/info\/index.php\/?p=425"},"modified":"2009-10-07T06:04:57","modified_gmt":"2009-10-07T06:04:57","slug":"find-and-replace","status":"publish","type":"post","link":"https:\/\/g33kinfo.com\/info\/find-and-replace\/","title":{"rendered":"Find and Replace"},"content":{"rendered":"<p>When you are working on the Linux command line and you come across a large file or a large number of files in which you need to replace a certain text with another, finding and pasting over each instance of the text can be a bit time consuming. Well, worry no more. Linux has just the solution for you. Here\u2019s a way to find and replace a string of text in one or more files automatically.<\/p>\n<p>For the purpose of this exercise we will use a Linux command line tool called \u201csed\u201d.  \u201dsed\u201d is a very powerful and versatile tool, and a lot can be written about its capabilities. We are using a very limited aspect of \u201csed\u201d here. I would definitely recommend that you read up a little more on \u201csed\u201d if you find this aspect of it interesting.<\/p>\n<p>We are going to use the following syntax to find and replace a string of text in a file:<\/p>\n<p># sed -i \u2019s\/[orginal_text]\/[new_text]\/\u2019 filename.txt<\/p>\n<p>Say you have a file called \u201cdatabase.txt\u201d with numerous instances of the IP address of your database server in it. You have just switched to a new database server and need to update it with the new server\u2019s IP address. The old IP address is 192.168.1.16 and the new one is 192.168.1.22. Here\u2019s how you go about it:<\/p>\n<p># cat database.txt<br \/>\nLOCAL_DATABASE = 192.168.1.16<br \/>\nLOCAL_DIR = \/home\/calvin\/<br \/>\nPROD_DB = 192.168.1.16<\/p>\n<p># sed -i \u2019s\/192.168.1.16\/192.168.1.22\/g\u2019 database.txt<br \/>\n# cat database.txt<br \/>\nLOCAL_DATABASE = 192.168.1.22<br \/>\nLOCAL_DIR = \/home\/calvin\/<br \/>\nPROD_DB = 192.168.1.22<\/p>\n<p>Now open the file \u201cdatabase.inc\u201d and check to see if the new IP address has taken place of your old one. Here\u2019s the breakup of the above command. First you call the \u201csed\u201d command. Then you pass it the parameter \u201c-s\u201d which stands for \u201cin place of\u201d. Now we use a little bit of regular expressions, commonly known as \u201cregex\u201d  for the next bit. The \u201cs\u201d in the quoted string stands for \u201csubstitute\u201d, and the \u201cg\u201d at the end stands for \u201cglobal\u201d. Between them they result in a \u201cglobal substitution of the the string of text you place in between them.<\/p>\n<p>You can optionally skip the \u201cg\u201d at the end. This means that the substitution will not be global, which practically translates to the substitution of only the first instance of the string in a line. So if you had a line with multiple instances of the text you are trying to replace, here\u2019s what will happen<\/p>\n<p># cat database.txt<br \/>\nLOCAL_DATABASE = 192.168.1.16<br \/>\nLOCAL_DIR = \/home\/calvin\/<br \/>\nPROD_DB = 192.168.1.16, 192.168.1.16<\/p>\n<p># sed -i \u2019s\/192.168.1.16\/192.168.1.22\/\u2019 database.txt<br \/>\n# cat database.txt<br \/>\nLOCAL_DATABASE = 192.168.1.22<br \/>\nLOCAL_DIR = \/home\/calvin\/<br \/>\nPROD_DB = 192.168.1.22, 192.168.1.16<\/p>\n<p>Here comes the real magic. Now, say you want to change a string of text not just in a single file, but in the entire directory you are in. There are a number of text files in which you need to find and replace the \u201cwine\u201d with \u201cchampagne\u201d.<\/p>\n<p># find . -maxdepth 1 -name \u201c*.txt\u201d -type f -exec sed -i \u2019s\/wine\/champagne\/\u2019 {} \\<\/p>\n<p>We use the find command to get a list of all the text files in the current directory. That\u2019s the \u201cfind . -maxdepth 1 -name \u201c*.txt\u201d -type f\u201d part. \u201cfind . maxdepth 1? tell the computer to look in the current directory and go no deeper than the current directory. The \u2018-name  \u201d*.txt\u201d\u2018 part tells find to only list files with the extension of \u201c.txt\u201d. Then the \u201c-type f\u201d section specifies that \u201cfind\u201d should only pick exactly matching files. Finally the \u201c-exec\u201d part tells \u201cfind\u201d to execute the command that follows, which, in this case, is the \u201csed\u201d command to replace the text \u2013 \u201csed -i \u2019s\/wine\/champagne\/\u2019 {} \\\u201d.<\/p>\n<p>I realize that the above command seems complicated. However, once you use it a little bit you will realize that it is probably worth noting it down and using it. Now try changing a string of text in multiple levels of directories.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you are working on the Linux command line and you come across a large file or a large number of files in which you need to replace a certain text with another, finding and pasting over each instance of the text can be a bit time consuming. Well, worry no more. Linux has just&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/g33kinfo.com\/info\/find-and-replace\/\">Read More<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-425","post","type-post","status-publish","format-standard","hentry","category-info"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Find and Replace - Linux Shtuff<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/g33kinfo.com\/info\/find-and-replace\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Find and Replace - Linux Shtuff\" \/>\n<meta property=\"og:description\" content=\"When you are working on the Linux command line and you come across a large file or a large number of files in which you need to replace a certain text with another, finding and pasting over each instance of the text can be a bit time consuming. Well, worry no more. Linux has just... Read More\" \/>\n<meta property=\"og:url\" content=\"https:\/\/g33kinfo.com\/info\/find-and-replace\/\" \/>\n<meta property=\"og:site_name\" content=\"Linux Shtuff\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/fb.me\/g33kinf0\" \/>\n<meta property=\"article:author\" content=\"https:\/\/fb.me\/g33kinf0\" \/>\n<meta property=\"article:published_time\" content=\"2009-10-07T06:04:57+00:00\" \/>\n<meta name=\"author\" content=\"g33kadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/drsinger1111\" \/>\n<meta name=\"twitter:site\" content=\"@drsinger1111\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/find-and-replace\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/find-and-replace\\\/\"},\"author\":{\"name\":\"g33kadmin\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\"},\"headline\":\"Find and Replace\",\"datePublished\":\"2009-10-07T06:04:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/find-and-replace\\\/\"},\"wordCount\":653,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\"},\"articleSection\":[\"General Info\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/g33kinfo.com\\\/info\\\/find-and-replace\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/find-and-replace\\\/\",\"url\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/find-and-replace\\\/\",\"name\":\"Find and Replace - Linux Shtuff\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#website\"},\"datePublished\":\"2009-10-07T06:04:57+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/find-and-replace\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/g33kinfo.com\\\/info\\\/find-and-replace\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/find-and-replace\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Find and Replace\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#website\",\"url\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/\",\"name\":\"Linux Shtuff\",\"description\":\"Because I have CRS Syndrome...\",\"publisher\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\",\"name\":\"g33kadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\",\"url\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\",\"contentUrl\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\",\"width\":512,\"height\":512,\"caption\":\"g33kadmin\"},\"logo\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\"},\"description\":\"I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com\\\/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....\",\"sameAs\":[\"https:\\\/\\\/thelinuxreport.com\",\"https:\\\/\\\/fb.me\\\/g33kinf0\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/drsinger1111\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Find and Replace - Linux Shtuff","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:\/\/g33kinfo.com\/info\/find-and-replace\/","og_locale":"en_US","og_type":"article","og_title":"Find and Replace - Linux Shtuff","og_description":"When you are working on the Linux command line and you come across a large file or a large number of files in which you need to replace a certain text with another, finding and pasting over each instance of the text can be a bit time consuming. Well, worry no more. Linux has just... Read More","og_url":"https:\/\/g33kinfo.com\/info\/find-and-replace\/","og_site_name":"Linux Shtuff","article_publisher":"https:\/\/fb.me\/g33kinf0","article_author":"https:\/\/fb.me\/g33kinf0","article_published_time":"2009-10-07T06:04:57+00:00","author":"g33kadmin","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/drsinger1111","twitter_site":"@drsinger1111","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/g33kinfo.com\/info\/find-and-replace\/#article","isPartOf":{"@id":"https:\/\/g33kinfo.com\/info\/find-and-replace\/"},"author":{"name":"g33kadmin","@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547"},"headline":"Find and Replace","datePublished":"2009-10-07T06:04:57+00:00","mainEntityOfPage":{"@id":"https:\/\/g33kinfo.com\/info\/find-and-replace\/"},"wordCount":653,"commentCount":0,"publisher":{"@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547"},"articleSection":["General Info"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/g33kinfo.com\/info\/find-and-replace\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/g33kinfo.com\/info\/find-and-replace\/","url":"https:\/\/g33kinfo.com\/info\/find-and-replace\/","name":"Find and Replace - Linux Shtuff","isPartOf":{"@id":"https:\/\/g33kinfo.com\/info\/#website"},"datePublished":"2009-10-07T06:04:57+00:00","breadcrumb":{"@id":"https:\/\/g33kinfo.com\/info\/find-and-replace\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/g33kinfo.com\/info\/find-and-replace\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/g33kinfo.com\/info\/find-and-replace\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/g33kinfo.com\/info\/"},{"@type":"ListItem","position":2,"name":"Find and Replace"}]},{"@type":"WebSite","@id":"https:\/\/g33kinfo.com\/info\/#website","url":"https:\/\/g33kinfo.com\/info\/","name":"Linux Shtuff","description":"Because I have CRS Syndrome...","publisher":{"@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/g33kinfo.com\/info\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547","name":"g33kadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif","url":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif","contentUrl":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif","width":512,"height":512,"caption":"g33kadmin"},"logo":{"@id":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif"},"description":"I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com\/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....","sameAs":["https:\/\/thelinuxreport.com","https:\/\/fb.me\/g33kinf0","https:\/\/x.com\/https:\/\/twitter.com\/drsinger1111"]}]}},"_links":{"self":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/posts\/425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/comments?post=425"}],"version-history":[{"count":0,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/posts\/425\/revisions"}],"wp:attachment":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/media?parent=425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/categories?post=425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/tags?post=425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}