sajjad has asked for the wisdom of the Perl Monks concerning the following question:

I have a file with following lines:
.comm _resultAvg,8,4 .comm _resultSwap,8,4 .comm _resultMax,20,4
I want to replace this pattern with:
.data _resultAvg: .data.32 0 .data.32 0 .data _resultSwap: .data.32 0 .data.32 0 .data _resultMax: .data.32 0 .data.32 0 .data.32 0 .data.32 0 .data.32 0
How to perform this task using regex? That is, how to find the numeric e.g. 20 and divide it by 4 and then put corresponding # of ".data.32 0" lines after starting with relevant symbol "_resultMax". regards

Replies are listed 'Best First'.
Re: replace a line with lines based on information extracted from these line using Perl
by tybalt89 (Monsignor) on Nov 04, 2018 at 15:04 UTC
    #!/usr/bin/perl # https://perlmonks.org/?node_id=1225199 use strict; use warnings; while( <DATA> ) { s!\s*\.comm\s+(_\w+),(\d+),(\d+)\n! ".data\n$1:\n" . " .data.32 0\n" x ($2/$3)!e; print; } __DATA__ .comm _resultAvg,8,4 .comm _resultSwap,8,4 .comm _resultMax,20,4
      Thanks alot it worked.
Re: replace a line with lines based on information extracted from these line using Perl
by Corion (Patriarch) on Nov 04, 2018 at 15:20 UTC
      It's nice to point that out, but I think, it's ok. Stackoverflow sucks. Anytime someone asks a question there, a moderator can shut down the conversation like a dictator and then no one is allowed to respond anymore.

        Yes, it's certainly OK and this site offers more in ways of refining the question than Stackoverflow. Still, I consider it polite to point out if a question has been asked elsewhere so that people don't spend unnecessary effort on an answer that has already been given elsewhere.