{"id":172,"date":"2012-08-14T14:31:46","date_gmt":"2012-08-14T14:31:46","guid":{"rendered":"http:\/\/blogs.gentoo.org\/titanofold\/?p=172"},"modified":"2012-08-14T14:31:46","modified_gmt":"2012-08-14T14:31:46","slug":"perl-conditiona-replace-depending-on-match","status":"publish","type":"post","link":"https:\/\/blogs.gentoo.org\/titanofold\/2012\/08\/14\/perl-conditiona-replace-depending-on-match\/","title":{"rendered":"Perl Conditional Replace Depending On Match"},"content":{"rendered":"<p>So, as I write patches for various awful build systems, one of the things I try to do is slim down the patch file and make it easier to read. Changing &#8216;foobar-1.orig&#8217; to just <em>a<\/em> and &#8216;foobar-1&#8217; to just <em>b<\/em> makes a difference. Rather than manually edit the patch file, I turn to my dear friend, the regex search-and-replace. But, it gets to be a bit tiring editing essentially the same regex twice to get nearly identical results.<!--more--><\/p>\n<p>Take for example this little snippet of a real patch:<\/p>\n<pre>diff -Naurw pgpool-II-3.2.0.orig\/pgpool.conf.sample pgpool-II-3.2.0\/pgpool.conf.sample\r\n--- pgpool-II-3.2.0.orig\/pgpool.conf.sample\u00a0\u00a0 \u00a02012-07-19 03:06:21.000000000 -0400\r\n+++ pgpool-II-3.2.0\/pgpool.conf.sample\u00a0\u00a0 \u00a02012-08-14 07:33:39.200695855 -0400\r\n@@ -31,10 +31,8 @@\r\n\u00a0port = 9999\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Port number\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # (change requires restart)\r\n-socket_dir = '\/tmp'\r\n+socket_dir = '@PGSQL_SOCKETDIR@'\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Unix domain socket path\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # (change requires restart)<\/pre>\n<p>Now, I&#8217;d like to have a line that doesn&#8217;t wrap, and I&#8217;d like to save a few bytes of precious disk space, by changing the &#8216;pgpool-II-3.2.0.orig&#8217; and &#8216;pgpool-II-3.2.0&#8217; bits into a shorter &#8216;a&#8217; and &#8216;b&#8217;, respectively.<\/p>\n<p>Previously, I&#8217;ve used sed, but I would run it twice. That&#8217;s a pain. I have to make sure I run the sed commands in the right order so I don&#8217;t shoot myself in the foot and have to go back to running diff again to produce the patch.<\/p>\n<p>There must be a better way.<\/p>\n<p>There is.<\/p>\n<p>Perl&#8217;s\u00a0 &#8220;easy things easy and <em>hard things<\/em> possible&#8221; attitude allowed me to arrive at a short one-liner that allows me accomplish the task:<\/p>\n<pre>perl -pi -e 's\/pgpool-II-3\\.2\\.0(\\.orig)?\/$1?a:b\/eg' run_paths.patch<\/pre>\n<p>The secret is in the <em>\/e<\/em> modifier. <em>\/e<\/em> evaluates the replacement portion of the search-and-replace expression. So, the match\/search portion looks for &#8216;pgpool-II-3.2.0&#8217; and if it has &#8216;.orig&#8217; it captures that and stores it into <em>$1<\/em>. The replacement portion evaluates <em>$1<\/em>. If it is a true value &#8212; not <em>undef<\/em>, an empty string, or <em>0<\/em> &#8212; the replacement string is <em>a<\/em>, otherwise it is <em>b<\/em>.<\/p>\n<p>This one-liner handily transforms the above patch into a logically identical, more aesthetically pleasing, lighter-weight version.<\/p>\n<pre>diff -Naurw a\/pgpool.conf.sample b\/pgpool.conf.sample\r\n--- a\/pgpool.conf.sample\u00a0\u00a0 \u00a02012-07-19 03:06:21.000000000 -0400\r\n+++ b\/pgpool.conf.sample\u00a0\u00a0 \u00a02012-08-14 07:33:39.200695855 -0400\r\n@@ -31,10 +31,8 @@\r\n\u00a0port = 9999\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Port number\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # (change requires restart)\r\n-socket_dir = '\/tmp'\r\n+socket_dir = '@PGSQL_SOCKETDIR@'\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Unix domain socket path\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # (change requires restart)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>So, as I write patches for various awful build systems, one of the things I try to do is slim down the patch file and make it easier to read. Changing &#8216;foobar-1.orig&#8217; to just a and &#8216;foobar-1&#8217; to just b makes a difference. Rather than manually edit the patch file, I turn to my dear &hellip; <a href=\"https:\/\/blogs.gentoo.org\/titanofold\/2012\/08\/14\/perl-conditiona-replace-depending-on-match\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Perl Conditional Replace Depending On Match<\/span><\/a><\/p>\n","protected":false},"author":136,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[8,7,4],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1tO5a-2M","_links":{"self":[{"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/posts\/172"}],"collection":[{"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/users\/136"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/comments?post=172"}],"version-history":[{"count":6,"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/posts\/172\/revisions"}],"predecessor-version":[{"id":178,"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/posts\/172\/revisions\/178"}],"wp:attachment":[{"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/media?parent=172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/categories?post=172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/tags?post=172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}