{"id":5963,"date":"2022-08-31T17:21:35","date_gmt":"2022-08-31T11:51:35","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\/"},"modified":"2022-08-31T17:21:35","modified_gmt":"2022-08-31T11:51:35","slug":"solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\/","title":{"rendered":"[Solved] How to do multiple value replacement in a multiple line at one time"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-39803814\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"39803814\" data-parentid=\"39798083\" 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>By now the question has assumed form. I take it that you want to change the number in <code>{ }<\/code> on a particular line in the file. The posted code got close and I&#8217;ll comment on basics and complete it.<\/p>\n<p>Since we now find the number inside <code>{ }<\/code> there is no need for the hard-coded <code>$old_value<\/code>. In order to identify the desired line you need to match the <code>$old_parameter<\/code> so the condition becomes <code>if (\/$old_parameter\/)<\/code>. A comment on <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/perldoc.perl.org\/perlretut.html#Simple-word-matching\">regex matching<\/a> follows, related only to what is used in the code. Please read documentation and books for more.<\/p>\n<p>Consider <code>while (my $line = &lt;$fh&gt;)<\/code>. A line is read from the file via <code>$fh<\/code> by the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/perldoc.perl.org\/perlop.html#I%2fO-Operators\"><em>diamond operator <code>&lt;&gt;<\/code><\/em><\/a> and assigned to a variable <code>$line<\/code>. If you leave out the variable and write only <code>while (&lt;$fh&gt;)<\/code> then the line is assigned to a special variable called <code>$_<\/code>.  This variable is often used as a default in Perl. See <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/perldoc.perl.org\/perlvar.html#General-Variables\">General Variables<\/a>.<\/p>\n<p>To check whether a pattern is in a variable, to &#8220;<em>match<\/em>&#8221; it, we say <code>$var =~ m\/$patt\/<\/code>. This returns true or false in <em>scalar context<\/em> while in <em>list context<\/em> it returns the matches. See <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/perldoc.perl.org\/perlretut.html#Extracting-matches\">Extracting Matches<\/a>. A pattern is best placed into a variable by <code>$patt = qr(...)<\/code>. Our line is in <code>$_<\/code> so we need <code>$_ =~ m\/$patt\/<\/code>, where <code>m<\/code> may be omitted. Regex also allows a shortcut, since it works by default on <code>$_<\/code>, and we may say <code>\/$patt\/<\/code>.  Thus <code>if (\/...\/)<\/code>.  <\/p>\n<p>Now consider <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/perldoc.perl.org\/perlretut.html#Using-regular-expressions-in-Perl\">substitution<\/a>. To find a pattern and replace it we say <code>$var =~ s\/$patt\/$repl\/<\/code>. This changes <code>$var<\/code> &#8220;<em>in-place<\/em>&#8220;, meaning that after that statement <code>$var<\/code> is changed. If the <code>$patt<\/code> wasn&#8217;t found in <code>$var<\/code> nothing happens to it. With our line in <code>$_<\/code> we again need <code>$_<\/code> instead of <code>$var<\/code>, but the same shortcut works and we can say <code>s\/$patt\/$repl\/<\/code>.<\/p>\n<p>Your code has this \u2013 but it doesn&#8217;t do anything with it. The result is never given by the program. An easy way is to print every line and the output can be redirected to a file. Or write lines to a file.<\/p>\n<p>Now for the needed regex.  You want a number inside of <code>{ }<\/code>.  According to the data you show it is the only such pattern in the line. Then this will do<\/p>\n<pre><code>s\/ \\{ \\s* \\d+ \\s* \\} \/{$new_value}\/x;\n<\/code><\/pre>\n<p>The <code>\/x<\/code> allows us to use spaces for readibilty. (Otherwise they would be looked for in the string!)  A digit is matched by <code>\\d<\/code>, and <code>+<\/code> means all that come together, but at least one. In <code>a123b<\/code> it matches <code>123<\/code>, in <code>a12b3c<\/code> it matches <code>12<\/code>. The <code>{<\/code> and <code>}<\/code> are escaped since they have a special meaning in a regex. The <code>\\s*<\/code> allows for any number of spaces, or for none. <\/p>\n<p>The replacement side of the regex says to replace all that has been matched with <code>{$new_value}<\/code>.  There we don&#8217;t have to escape <code>{<\/code>, <code>}<\/code>. If you need to <em>capture<\/em> (remember) what has been matched place the pattern in between <code>()<\/code>. Here you can say<\/p>\n<pre><code>s\/ (\\{ \\s*) (\\d+) (\\s* \\}) \/$1$new_value$3\/x;\n<\/code><\/pre>\n<p>and have the original spaces preserved. The first capture is stored in <code>$1<\/code>, the second in <code>$2<\/code>, etc. If the <code>$new_value<\/code> changes during the loop you can compute it before the substitution.<\/p>\n<p>Please see the answer by Schwern and the technique offered in a comment by ikegami.<\/p>\n<p>Then we only need to print the line.  The full program<\/p>\n<pre><code>use warnings 'all';\nuse strict;\n\nmy $old_parameter=\"stack overflow version\";\nmy $new_value = 20;\n\nmy $filename=\"input.txt\";\n\nopen my $fh, \"&lt;\", $filename or die \"Can't open $filename: $!\";\n\nwhile ( &lt;$fh&gt; ) \n{\n    if (\/$old_parameter\/) \n    {\n        s\/\\{\\s* (\\d+) \\s*\\}\/{$new_value}\/x;\n    }\n    print;\n}\n<\/code><\/pre>\n<p>The <code>print;<\/code> uses the same default, <code>$_<\/code>, and means <code>print $_;<\/code>.  It is after the condition so that all lines are printed, changed or not. A few other errors have been fixed. An amusing one is the following: your <code>$old_parameter<\/code> has &#8220;<em>stack over<strong>folw<\/strong> version<\/em>&#8221; (misspelled) so it <em>never<\/em> matches the line.<\/p>\n<p>Finally, please read through <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/perldoc.perl.org\/perlretut.html\">perlretut<\/a>, or better yet, through a nice chapter on regular expressions from <em>a book or a tutorial that you are working with<\/em>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">6<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How to do multiple value replacement in a multiple line at one time <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] By now the question has assumed form. I take it that you want to change the number in { } on a particular line in the file. The posted code got close and I&#8217;ll comment on basics and complete it. Since we now find the number inside { } there is no need for &#8230; <a title=\"[Solved] How to do multiple value replacement in a multiple line at one time\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\/\" aria-label=\"More on [Solved] How to do multiple value replacement in a multiple line at one time\">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":[442],"class_list":["post-5963","post","type-post","status-publish","format-standard","hentry","category-solved","tag-perl"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] How to do multiple value replacement in a multiple line at one time - 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-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How to do multiple value replacement in a multiple line at one time - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] By now the question has assumed form. I take it that you want to change the number in { } on a particular line in the file. The posted code got close and I&#8217;ll comment on basics and complete it. Since we now find the number inside { } there is no need for ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-31T11:51:35+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=\"3 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-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How to do multiple value replacement in a multiple line at one time\",\"datePublished\":\"2022-08-31T11:51:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\\\/\"},\"wordCount\":592,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"perl\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\\\/\",\"name\":\"[Solved] How to do multiple value replacement in a multiple line at one time - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-08-31T11:51:35+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How to do multiple value replacement in a multiple line at one time\"}]},{\"@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\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"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 to do multiple value replacement in a multiple line at one time - 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-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How to do multiple value replacement in a multiple line at one time - JassWeb","og_description":"[ad_1] By now the question has assumed form. I take it that you want to change the number in { } on a particular line in the file. The posted code got close and I&#8217;ll comment on basics and complete it. Since we now find the number inside { } there is no need for ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\/","og_site_name":"JassWeb","article_published_time":"2022-08-31T11:51:35+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How to do multiple value replacement in a multiple line at one time","datePublished":"2022-08-31T11:51:35+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\/"},"wordCount":592,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["perl"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\/","name":"[Solved] How to do multiple value replacement in a multiple line at one time - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-31T11:51:35+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-do-multiple-value-replacement-in-a-multiple-line-at-one-time\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How to do multiple value replacement in a multiple line at one time"}]},{"@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\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","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\/5963","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=5963"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/5963\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=5963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=5963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=5963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}