{"id":245,"date":"2011-06-14T23:37:51","date_gmt":"2011-06-15T06:37:51","guid":{"rendered":"http:\/\/blog.networkpresence.co\/?p=245"},"modified":"2011-06-15T07:27:04","modified_gmt":"2011-06-15T14:27:04","slug":"using-the-unix-command-sed-with-urls","status":"publish","type":"post","link":"http:\/\/blog.networkpresence.co\/?p=245","title":{"rendered":"Using the Unix command &#8216;sed&#8217; with URLs"},"content":{"rendered":"<p>I&#8217;m an &#8220;old school&#8221; Unix user, who grew up on sed, awk &#038; Sendmail, but many people still use sed &#038; its syntax from the Unix or Linux command-line daily, so this is a quick note on how to use &#8216;sed&#8217; when you&#8217;re working with URLs.<\/p>\n<p>The &#8216;trick&#8217; here (if there is one) is that most sed examples are for using it to substitute characters in a text line or file.<br \/>\neg: changing http:\/\/this-site\/ to http:\/\/that-site\/<br \/>\nBut the usual delimiter of the text to be changed is the &#8216;\/&#8217; (slash or divide-sign) character, which simply doesn&#8217;t work when the text your substituting also contains the &#8216;\/&#8217; (ie: URLs).<\/p>\n<p>Please you don&#8217;t have to use the &#8216;\/&#8217; to delimit search &#038; replace text in sed command lines, you can use (just about\/within reason) any single character you like (eg: the &#8216;%&#8217; (percent-sign) character), just as long as you use it consistently in your sed command line.<\/p>\n<p>So if you&#8217;re using sed to substitute text in a URL, use the % to delimit the text to search for and the text to replace it with.<br \/>\nHere&#8217;s some examples:<\/p>\n<p>Eg 1: We want to change all references of http:\/\/google.com to https:\/\/google.com in an inputfile, so a sed command line for this may be:<\/p>\n<p><code>sed 's%http:\/\/google.com%https:\/\/google.com%g' inputfile > outputfile<br \/>\n<\/code><br \/>\nEg 2: You have a shell variable containing a URL, say $thisurl which you want to change to the contents of another variable $thaturl :<\/p>\n<p><code>sed \"s%$thisurl%$thaturl%g\"<br \/>\n<\/code><br \/>\nNote: the use of the double-quotes (&#8220;) character to allow shell variable expansion within the sed command.<\/p>\n<p>Don&#8217;t forget that &#8220;man sed&#8221; is your friend &#038; there&#8217;s an O&#8217;Reilly &#8220;sed&#8221; book too on it for much &#8220;sed goodness&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m an &#8220;old school&#8221; Unix user, who grew up on sed, awk &#038; Sendmail, but many people still use sed &#038; its syntax from the Unix or Linux command-line daily, so this is a quick note on how to use &hellip; <a href=\"http:\/\/blog.networkpresence.co\/?p=245\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[46,17],"class_list":["post-245","post","type-post","status-publish","format-standard","hentry","category-network-presence","tag-sed","tag-unix"],"_links":{"self":[{"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=\/wp\/v2\/posts\/245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=245"}],"version-history":[{"count":3,"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=\/wp\/v2\/posts\/245\/revisions"}],"predecessor-version":[{"id":262,"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=\/wp\/v2\/posts\/245\/revisions\/262"}],"wp:attachment":[{"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.networkpresence.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}