{"id":5079,"date":"2016-09-22T01:52:10","date_gmt":"2016-09-22T01:52:10","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=5079"},"modified":"2016-09-22T01:52:10","modified_gmt":"2016-09-22T01:52:10","slug":"replace-files-recursively-sed","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/replace-files-recursively-sed\/","title":{"rendered":"Replace in files recursively with sed"},"content":{"rendered":"<p>Replacing text in many files is reasonably simple with sed, on the command line.<\/p>\n<p>If you do this, I would recommend that you come up with a way to take a backup, because there is almost no chance you&#8217;ll get it right on the first try.<\/p>\n<p>My preferred approach is to make an empty git repository with the initial state, which allows me to rollback the changes every time I need to iterate on the sed script.<\/p>\n<pre lang=\"bash\">\ngit init .\ngit add -A \ngit commit -m \"files\"\n<\/pre>\n<p>For this example, I&#8217;m adding an Amazon link to a bunch of files. The successful solution is as follows (you want the generate format of &#8220;s\/<search>\/<replacement>\/&#8221; to control it in your own script).<\/p>\n<pre lang=\"bash\">\nfind . -type f -iname '*.html' -exec sed -i 's\/\"sidebar-module\">\/\"sidebar-module\"><iframe style=\"width:120px;height:240px;\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" frameborder=\"0\" src=\"\\\/\\\/ws-na.amazon-adsystem.com\\\/widgets\\\/q?ServiceVersion=20070822\\&#038;OneJS=1\\&#038;Operation=GetAdHtml\\&#038;MarketPlace=US\\&#038;source=ss\\&#038;ref=as_ss_li_til\\&#038;ad_type=product_link\\&#038;tracking_id=thesecrelifeo-20\\&#038;marketplace=amazon\\&#038;region=US\\&#038;placement=0981531687\\&#038;asins=0981531687\\&#038;linkId=0dc4250b21bf0e833004fd3e58765806\\&#038;show_border=true\\&#038;link_opens_in_new_window=true\"><\\\/iframe>\/' \"{}\" +;\n<\/pre>\n<p>Once you create this script, you should check it in.<\/p>\n<pre lang=\"bash\">\n...add script...\ngit add replace.sh\ngit commit -m \"script\"\n<\/pre>\n<p>I found there were several problems getting this to work. You need to escape &#038; and \\, and make sure to replace what you removed &#8211; it&#8217;s often helpful to use a chunk of text that comes before or after the replacement.<\/p>\n<p>When you make a mistake, you can just do<\/p>\n<pre lang=\"bash\">\ngit reset --hard\n<\/pre>\n<p>As long as you&#8217;re committing the replacement script as you go.<\/p>\n<p>If you like, you can get rid of the git repository once you&#8217;re done, by doing this:<\/p>\n<pre lang=\"bash\">\nrm -rf .git\n<\/pre>\n<p>~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to replace text in a large number of files on the command line<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","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,"footnotes":""},"categories":[12],"tags":[249,459,500],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/5079"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=5079"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/5079\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=5079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=5079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=5079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}