{"id":16372,"date":"2022-10-15T10:10:45","date_gmt":"2022-10-15T04:40:45","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/"},"modified":"2022-10-15T10:10:45","modified_gmt":"2022-10-15T04:40:45","slug":"solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/","title":{"rendered":"[Solved] How can I find and replace all forms of pow(var,2) with square(var)?"},"content":{"rendered":"<p> [ad_1]<br \/>\n[*]<\/p>\n<div id=\"answer-64067200\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"64067200\" data-parentid=\"64053771\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Provisos and assumptions:<\/p>\n<ul>\n<li>OP mentions needing to process multiple files; for this answer I&#8217;m going to focus on a single file; OP can start another question if issues arise with a multi-file solution<\/li>\n<li>OP mentions wanting to <code>replace<\/code> some strings but it&#8217;s not clear (to me) if the original file is to be overwritten or a new file is to be created; for this answer I&#8217;m going to focus on generating the &#8216;modified&#8217; output; OP can expand on this solution (below) based on final requirements<\/li>\n<li>examples seem to imply 4x different search patterns (<code>alpha<\/code>, <code>beta<\/code>, <code>betaR_red<\/code>, <code>epsilon_gamma<\/code>); I&#8217;m going to assume there could be a variable number of patterns that need to be searched for<\/li>\n<li>for simplicity sake I&#8217;m going to assume the search patterns are stored in an array<\/li>\n<li>search patterns contain no leading\/trailing white space<\/li>\n<li>search patterns are relatively simple and do <strong>not<\/strong> contain any special characters (eg, line feeds)<\/li>\n<\/ul>\n<p>Sample input file:<\/p>\n<pre><code>$ cat input.txt\npow(alpha,2) + pow(beta,2)\n(3*pow(betaR_red,2))\n2\/pow(gammaBlue,3))\n-pow(epsilon_gamma,2)+5\n<\/code><\/pre>\n<p>Array of search patterns:<\/p>\n<pre><code>$ var=(alpha beta betaR_red epsilon_gamma 'double helix')\n$ typeset -p var\ndeclare -a var=([0]=\"alpha\" [1]=\"beta\" [2]=\"betaR_red\" [3]=\"epsilon_gamma\" [4]=\"double helix\")\n<\/code><\/pre>\n<p>The general idea is to use <code>sed<\/code> to do a multi-pattern search of the file based on the contents of the <code>var[]<\/code> array.  This means we need a way to reference the array in a manner that will be suitable for a <code>sed<\/code> multi-pattern match (ie, values need to be separated by a pipe (<code>|<\/code>).<\/p>\n<p>By assigning <code>IFS='|'<\/code> we can &#8216;reformat&#8217; the array contents to work as a multi-pattern search string for <code>sed<\/code>:<\/p>\n<pre><code>$ echo \"${var[*]}\"\nalpha beta betaR_red epsilon_gamma double helix\n$ IFS='|' varX=\"${var[*]}\" ; echo \"${varX}\"\nalpha|beta|betaR_red|epsilon_gamma|double helix\n<\/code><\/pre>\n<p>Which brings us to the <code>sed<\/code> command:<\/p>\n<pre><code>$ IFS='|' sed -E \"s\/pow\\((${var[*]}),2\\)\/square(\\1)\/g\" input.txt\n<\/code><\/pre>\n<p>Where:<\/p>\n<ul>\n<li><code>sed -E<\/code> &#8211; run with extended regex support<\/li>\n<li><code>pow\\(<\/code> \/ <code>,2\\)<\/code> &#8211; search for our <code>pow(..,2)<\/code> string, escaping the parens so they are not evaluated as delimiters of a regex group<\/li>\n<li><code>IFS='|'<\/code> \/ <code>(${var[*]})<\/code> &#8211; expand array <code>var<\/code> using <code>'|'<\/code> as value delimiter; by wrapping in parens this becomes our first (and only) search group<\/li>\n<li><code>square(<\/code> \/ <code>)<\/code> &#8211; replacement string for <code>pow(<\/code> \/ <code>,2)<\/code> pattern<\/li>\n<li><code>\\1<\/code> &#8211; copy contents of our search group, eg, if we matched on <code>pow(beta,2)<\/code> then <code>\\1<\/code> == <code>beta<\/code><\/li>\n<\/ul>\n<p>If we execute the above as <code>set -xv ; IFS='|' sed ...; set +xv<\/code> we will generate the following &#8216;debug&#8217; output showing how the <code>sed<\/code> command is expanded with the values of the <code>var<\/code> array:<\/p>\n<pre><code>++ IFS='|'\n++ sed -E 's\/pow\\((alpha|beta|betaR_red|epsilon_gamma|double helix),2\\)\/square(\\1)\/g' input.txt\n<\/code><\/pre>\n<p>The actual output of the above <code>sed<\/code> command:<\/p>\n<pre><code>square(alpha) + square(beta)          # 2x changes\n(3*square(betaR_red))                 # 1x change\n2\/pow(gammaBlue,3))                   # no changes\n-square(epsilon_gamma)+5              # 1x change\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[*][ad_2]<\/p>\n<p>solved How can I find and replace all forms of pow(var,2) with square(var)? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] [*] Provisos and assumptions: OP mentions needing to process multiple files; for this answer I&#8217;m going to focus on a single file; OP can start another question if issues arise with a multi-file solution OP mentions wanting to replace some strings but it&#8217;s not clear (to me) if the original file is to be &#8230; <a title=\"[Solved] How can I find and replace all forms of pow(var,2) with square(var)?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/\" aria-label=\"More on [Solved] How can I find and replace all forms of pow(var,2) with square(var)?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[1042,347,1212,444],"class_list":["post-16372","post","type-post","status-publish","format-standard","hentry","category-solved","tag-bash","tag-regex","tag-replace","tag-sed"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How can I find and replace all forms of pow(var,2) with square(var)? - JassWeb<\/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:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How can I find and replace all forms of pow(var,2) with square(var)? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] [*] Provisos and assumptions: OP mentions needing to process multiple files; for this answer I&#8217;m going to focus on a single file; OP can start another question if issues arise with a multi-file solution OP mentions wanting to replace some strings but it&#8217;s not clear (to me) if the original file is to be ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-15T04:40:45+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How can I find and replace all forms of pow(var,2) with square(var)?\",\"datePublished\":\"2022-10-15T04:40:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/\"},\"wordCount\":360,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"bash\",\"regex\",\"replace\",\"sed\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/\",\"name\":\"[Solved] How can I find and replace all forms of pow(var,2) with square(var)? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-15T04:40:45+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How can I find and replace all forms of pow(var,2) with square(var)?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How can I find and replace all forms of pow(var,2) with square(var)? - JassWeb","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:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How can I find and replace all forms of pow(var,2) with square(var)? - JassWeb","og_description":"[ad_1] [*] Provisos and assumptions: OP mentions needing to process multiple files; for this answer I&#8217;m going to focus on a single file; OP can start another question if issues arise with a multi-file solution OP mentions wanting to replace some strings but it&#8217;s not clear (to me) if the original file is to be ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/","og_site_name":"JassWeb","article_published_time":"2022-10-15T04:40:45+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How can I find and replace all forms of pow(var,2) with square(var)?","datePublished":"2022-10-15T04:40:45+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/"},"wordCount":360,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["bash","regex","replace","sed"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/","url":"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/","name":"[Solved] How can I find and replace all forms of pow(var,2) with square(var)? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-15T04:40:45+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-find-and-replace-all-forms-of-powvar2-with-squarevar\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How can I find and replace all forms of pow(var,2) with square(var)?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16372","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=16372"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16372\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=16372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=16372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=16372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}