Search and replace using a custom utility

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I want to run a utility for replace values matching a regular expression. This means for every match of a regular expression, call the utility with the characters comprising the match. The output of the utility replaces the original characters.



For illustration purposes using factor:



$ factor 230
230: 2 5 23


So using this utility, I want to pick out the integers, call factor with the integer, and replace the original integer with the output of factor.



This is what I would expect on a sample input:



$ [code] <<< "Given the numbers with factors: 27, 13, 230, and 19, it is evident which are primes."
Given the numbers with factors: 27: 3 3 3, 13: 13, 230: 2 5 23, and 19: 19, it is evident which are primes.


I thought this might work, but it looks like it's trying to interpret the input directly. Using sed (GNU sed) 4.2.2.



$ sed -E 's/([0-9]+)/factor 1/ge' <<< 'Given the numbers with factors: 27, 13, 230, and 19, it is evident which are primes.'
sh: 1: Given: not found


Obviously I'm not understanding what the e flag does. Removing the e shows that the regular expression is correctly pulling out the integers and passing them as 1.



I tried doing this in awk:



$ awk 'r = gensub(/([0-9]+)/, system("factor \1"), "g"); print r' <<< 'Given the numbers with factors: 27, 13, 230, and 19, it is evident which are primes.'
1:
Given the numbers with factors: 0, 0, 0, and 0, it is evident which are primes.


I'm not sure where the 1: comes from, but it's apparent that it's printing just the return code from system. There doesn't appear to be a way to capture the standard output from a command in awk.



Is what I'm asking for possible using the core utilities?







share|improve this question





















  • elaborate your for every match in the input, because for now you are reversing all words. Post a testable input which would contain matched and non-matched items
    – RomanPerekhrest
    Jul 18 at 13:48











  • @RomanPerekhrest – For every match of a regular expression, call a custom utility with the characters comprising the match. The output of the custom utility replaces the original characters.
    – Yimin Rong
    Jul 18 at 13:52










  • again, Post a testable input which would contain matched and non-matched items. Also, elaborate if a regex pattern could cover a part of the word or the whole word only
    – RomanPerekhrest
    Jul 18 at 13:57











  • @RomanPerekhrest - Replaced reverse with factor. People are fixated on this red herring.
    – Yimin Rong
    Jul 18 at 14:39
















up vote
1
down vote

favorite












I want to run a utility for replace values matching a regular expression. This means for every match of a regular expression, call the utility with the characters comprising the match. The output of the utility replaces the original characters.



For illustration purposes using factor:



$ factor 230
230: 2 5 23


So using this utility, I want to pick out the integers, call factor with the integer, and replace the original integer with the output of factor.



This is what I would expect on a sample input:



$ [code] <<< "Given the numbers with factors: 27, 13, 230, and 19, it is evident which are primes."
Given the numbers with factors: 27: 3 3 3, 13: 13, 230: 2 5 23, and 19: 19, it is evident which are primes.


I thought this might work, but it looks like it's trying to interpret the input directly. Using sed (GNU sed) 4.2.2.



$ sed -E 's/([0-9]+)/factor 1/ge' <<< 'Given the numbers with factors: 27, 13, 230, and 19, it is evident which are primes.'
sh: 1: Given: not found


Obviously I'm not understanding what the e flag does. Removing the e shows that the regular expression is correctly pulling out the integers and passing them as 1.



I tried doing this in awk:



$ awk 'r = gensub(/([0-9]+)/, system("factor \1"), "g"); print r' <<< 'Given the numbers with factors: 27, 13, 230, and 19, it is evident which are primes.'
1:
Given the numbers with factors: 0, 0, 0, and 0, it is evident which are primes.


I'm not sure where the 1: comes from, but it's apparent that it's printing just the return code from system. There doesn't appear to be a way to capture the standard output from a command in awk.



Is what I'm asking for possible using the core utilities?







share|improve this question





















  • elaborate your for every match in the input, because for now you are reversing all words. Post a testable input which would contain matched and non-matched items
    – RomanPerekhrest
    Jul 18 at 13:48











  • @RomanPerekhrest – For every match of a regular expression, call a custom utility with the characters comprising the match. The output of the custom utility replaces the original characters.
    – Yimin Rong
    Jul 18 at 13:52










  • again, Post a testable input which would contain matched and non-matched items. Also, elaborate if a regex pattern could cover a part of the word or the whole word only
    – RomanPerekhrest
    Jul 18 at 13:57











  • @RomanPerekhrest - Replaced reverse with factor. People are fixated on this red herring.
    – Yimin Rong
    Jul 18 at 14:39












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I want to run a utility for replace values matching a regular expression. This means for every match of a regular expression, call the utility with the characters comprising the match. The output of the utility replaces the original characters.



For illustration purposes using factor:



$ factor 230
230: 2 5 23


So using this utility, I want to pick out the integers, call factor with the integer, and replace the original integer with the output of factor.



This is what I would expect on a sample input:



$ [code] <<< "Given the numbers with factors: 27, 13, 230, and 19, it is evident which are primes."
Given the numbers with factors: 27: 3 3 3, 13: 13, 230: 2 5 23, and 19: 19, it is evident which are primes.


I thought this might work, but it looks like it's trying to interpret the input directly. Using sed (GNU sed) 4.2.2.



$ sed -E 's/([0-9]+)/factor 1/ge' <<< 'Given the numbers with factors: 27, 13, 230, and 19, it is evident which are primes.'
sh: 1: Given: not found


Obviously I'm not understanding what the e flag does. Removing the e shows that the regular expression is correctly pulling out the integers and passing them as 1.



I tried doing this in awk:



$ awk 'r = gensub(/([0-9]+)/, system("factor \1"), "g"); print r' <<< 'Given the numbers with factors: 27, 13, 230, and 19, it is evident which are primes.'
1:
Given the numbers with factors: 0, 0, 0, and 0, it is evident which are primes.


I'm not sure where the 1: comes from, but it's apparent that it's printing just the return code from system. There doesn't appear to be a way to capture the standard output from a command in awk.



Is what I'm asking for possible using the core utilities?







share|improve this question













I want to run a utility for replace values matching a regular expression. This means for every match of a regular expression, call the utility with the characters comprising the match. The output of the utility replaces the original characters.



For illustration purposes using factor:



$ factor 230
230: 2 5 23


So using this utility, I want to pick out the integers, call factor with the integer, and replace the original integer with the output of factor.



This is what I would expect on a sample input:



$ [code] <<< "Given the numbers with factors: 27, 13, 230, and 19, it is evident which are primes."
Given the numbers with factors: 27: 3 3 3, 13: 13, 230: 2 5 23, and 19: 19, it is evident which are primes.


I thought this might work, but it looks like it's trying to interpret the input directly. Using sed (GNU sed) 4.2.2.



$ sed -E 's/([0-9]+)/factor 1/ge' <<< 'Given the numbers with factors: 27, 13, 230, and 19, it is evident which are primes.'
sh: 1: Given: not found


Obviously I'm not understanding what the e flag does. Removing the e shows that the regular expression is correctly pulling out the integers and passing them as 1.



I tried doing this in awk:



$ awk 'r = gensub(/([0-9]+)/, system("factor \1"), "g"); print r' <<< 'Given the numbers with factors: 27, 13, 230, and 19, it is evident which are primes.'
1:
Given the numbers with factors: 0, 0, 0, and 0, it is evident which are primes.


I'm not sure where the 1: comes from, but it's apparent that it's printing just the return code from system. There doesn't appear to be a way to capture the standard output from a command in awk.



Is what I'm asking for possible using the core utilities?









share|improve this question












share|improve this question




share|improve this question








edited Jul 18 at 15:53
























asked Jul 18 at 13:16









Yimin Rong

2231415




2231415











  • elaborate your for every match in the input, because for now you are reversing all words. Post a testable input which would contain matched and non-matched items
    – RomanPerekhrest
    Jul 18 at 13:48











  • @RomanPerekhrest – For every match of a regular expression, call a custom utility with the characters comprising the match. The output of the custom utility replaces the original characters.
    – Yimin Rong
    Jul 18 at 13:52










  • again, Post a testable input which would contain matched and non-matched items. Also, elaborate if a regex pattern could cover a part of the word or the whole word only
    – RomanPerekhrest
    Jul 18 at 13:57











  • @RomanPerekhrest - Replaced reverse with factor. People are fixated on this red herring.
    – Yimin Rong
    Jul 18 at 14:39
















  • elaborate your for every match in the input, because for now you are reversing all words. Post a testable input which would contain matched and non-matched items
    – RomanPerekhrest
    Jul 18 at 13:48











  • @RomanPerekhrest – For every match of a regular expression, call a custom utility with the characters comprising the match. The output of the custom utility replaces the original characters.
    – Yimin Rong
    Jul 18 at 13:52










  • again, Post a testable input which would contain matched and non-matched items. Also, elaborate if a regex pattern could cover a part of the word or the whole word only
    – RomanPerekhrest
    Jul 18 at 13:57











  • @RomanPerekhrest - Replaced reverse with factor. People are fixated on this red herring.
    – Yimin Rong
    Jul 18 at 14:39















elaborate your for every match in the input, because for now you are reversing all words. Post a testable input which would contain matched and non-matched items
– RomanPerekhrest
Jul 18 at 13:48





elaborate your for every match in the input, because for now you are reversing all words. Post a testable input which would contain matched and non-matched items
– RomanPerekhrest
Jul 18 at 13:48













@RomanPerekhrest – For every match of a regular expression, call a custom utility with the characters comprising the match. The output of the custom utility replaces the original characters.
– Yimin Rong
Jul 18 at 13:52




@RomanPerekhrest – For every match of a regular expression, call a custom utility with the characters comprising the match. The output of the custom utility replaces the original characters.
– Yimin Rong
Jul 18 at 13:52












again, Post a testable input which would contain matched and non-matched items. Also, elaborate if a regex pattern could cover a part of the word or the whole word only
– RomanPerekhrest
Jul 18 at 13:57





again, Post a testable input which would contain matched and non-matched items. Also, elaborate if a regex pattern could cover a part of the word or the whole word only
– RomanPerekhrest
Jul 18 at 13:57













@RomanPerekhrest - Replaced reverse with factor. People are fixated on this red herring.
– Yimin Rong
Jul 18 at 14:39




@RomanPerekhrest - Replaced reverse with factor. People are fixated on this red herring.
– Yimin Rong
Jul 18 at 14:39










4 Answers
4






active

oldest

votes

















up vote
1
down vote













Possible in Perl. Using split, map, scalar, lc, reverse, ucfirst, join. A bit more complex than I originally thought because of the commas and context sensitivity of matching.



#!/usr/bin/perl
use warnings;
use strict;

sub rev
my ($string) = @_;
my @words = split /(W+)/, $string;
my @caps = map scalar(/^[[:upper:]]/), @words;
$_ = /w/ ? lc reverse : $_ for @words;
shift @caps and $_ = ucfirst for @words;
return join "", @words


use Test::More tests => 1;
my $r = rev('Hello, my name is Yimin Rong.');
is $r, 'Olleh, ym eman si Nimiy Gnor.';





share|improve this answer





















  • Thanks, but I used reverse just as an illustration. The actual utility I need to use is much more complex. Imagine reverse as a black box not easily duplicated in a scripting language.
    – Yimin Rong
    Jul 18 at 13:35










  • The trick is to write reverse in Perl, too ;-)
    – choroba
    Jul 18 at 13:42










  • Trust me, what I'm using isn't easy in perl, think about a factoring utility using the general number field sieve.
    – Yimin Rong
    Jul 18 at 13:46











  • @YiminRong: If it's easy in shell, it must be easy in Perl, too.
    – choroba
    Jul 18 at 14:11










  • Rewrote to illustrate with factor. Good luck writing that in perl!
    – Yimin Rong
    Jul 18 at 15:06

















up vote
0
down vote













Try the below awk solution:



echo "Hello" | awk ' for ( i=length($0);i>0;i-- ) if (i == length($0) ) printf "%s",toupper(substr($0,i,1)) else printf "%s",tolower(substr($0,i,1)) printf "n" '


Take each letter of the word character by character stating from the last. If it is the last character, convert to upper case and print otherwise print lower case.






share|improve this answer




























    up vote
    0
    down vote













    Run the following :



     perl -pe 's/(d+)/qx(factor $1) =~ s|n||r/ge' input-file.txt


    The reason why your sed command is not working is because sed will execute the entire pattern space unlike Perl which is aligned with how you were thinking and executes just as much as is on the rhs of the s/// command and replaces tonly that much with the command output.



    And that's precisely why if you notice sed was cribbing abt "Given:" utility not found. Given was the beginning of your pattern space. HTH






    share|improve this answer






























      up vote
      -1
      down vote













      Run the following:



      perl -pe 's/(d+)/qx(factor $1) =~ s|n||r/ge' input-file.txt





      share|improve this answer























      • Care to explain how this works?
        – Kusalananda
        Jul 20 at 19:08










      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f456991%2fsearch-and-replace-using-a-custom-utility%23new-answer', 'question_page');

      );

      Post as a guest






























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      Possible in Perl. Using split, map, scalar, lc, reverse, ucfirst, join. A bit more complex than I originally thought because of the commas and context sensitivity of matching.



      #!/usr/bin/perl
      use warnings;
      use strict;

      sub rev
      my ($string) = @_;
      my @words = split /(W+)/, $string;
      my @caps = map scalar(/^[[:upper:]]/), @words;
      $_ = /w/ ? lc reverse : $_ for @words;
      shift @caps and $_ = ucfirst for @words;
      return join "", @words


      use Test::More tests => 1;
      my $r = rev('Hello, my name is Yimin Rong.');
      is $r, 'Olleh, ym eman si Nimiy Gnor.';





      share|improve this answer





















      • Thanks, but I used reverse just as an illustration. The actual utility I need to use is much more complex. Imagine reverse as a black box not easily duplicated in a scripting language.
        – Yimin Rong
        Jul 18 at 13:35










      • The trick is to write reverse in Perl, too ;-)
        – choroba
        Jul 18 at 13:42










      • Trust me, what I'm using isn't easy in perl, think about a factoring utility using the general number field sieve.
        – Yimin Rong
        Jul 18 at 13:46











      • @YiminRong: If it's easy in shell, it must be easy in Perl, too.
        – choroba
        Jul 18 at 14:11










      • Rewrote to illustrate with factor. Good luck writing that in perl!
        – Yimin Rong
        Jul 18 at 15:06














      up vote
      1
      down vote













      Possible in Perl. Using split, map, scalar, lc, reverse, ucfirst, join. A bit more complex than I originally thought because of the commas and context sensitivity of matching.



      #!/usr/bin/perl
      use warnings;
      use strict;

      sub rev
      my ($string) = @_;
      my @words = split /(W+)/, $string;
      my @caps = map scalar(/^[[:upper:]]/), @words;
      $_ = /w/ ? lc reverse : $_ for @words;
      shift @caps and $_ = ucfirst for @words;
      return join "", @words


      use Test::More tests => 1;
      my $r = rev('Hello, my name is Yimin Rong.');
      is $r, 'Olleh, ym eman si Nimiy Gnor.';





      share|improve this answer





















      • Thanks, but I used reverse just as an illustration. The actual utility I need to use is much more complex. Imagine reverse as a black box not easily duplicated in a scripting language.
        – Yimin Rong
        Jul 18 at 13:35










      • The trick is to write reverse in Perl, too ;-)
        – choroba
        Jul 18 at 13:42










      • Trust me, what I'm using isn't easy in perl, think about a factoring utility using the general number field sieve.
        – Yimin Rong
        Jul 18 at 13:46











      • @YiminRong: If it's easy in shell, it must be easy in Perl, too.
        – choroba
        Jul 18 at 14:11










      • Rewrote to illustrate with factor. Good luck writing that in perl!
        – Yimin Rong
        Jul 18 at 15:06












      up vote
      1
      down vote










      up vote
      1
      down vote









      Possible in Perl. Using split, map, scalar, lc, reverse, ucfirst, join. A bit more complex than I originally thought because of the commas and context sensitivity of matching.



      #!/usr/bin/perl
      use warnings;
      use strict;

      sub rev
      my ($string) = @_;
      my @words = split /(W+)/, $string;
      my @caps = map scalar(/^[[:upper:]]/), @words;
      $_ = /w/ ? lc reverse : $_ for @words;
      shift @caps and $_ = ucfirst for @words;
      return join "", @words


      use Test::More tests => 1;
      my $r = rev('Hello, my name is Yimin Rong.');
      is $r, 'Olleh, ym eman si Nimiy Gnor.';





      share|improve this answer













      Possible in Perl. Using split, map, scalar, lc, reverse, ucfirst, join. A bit more complex than I originally thought because of the commas and context sensitivity of matching.



      #!/usr/bin/perl
      use warnings;
      use strict;

      sub rev
      my ($string) = @_;
      my @words = split /(W+)/, $string;
      my @caps = map scalar(/^[[:upper:]]/), @words;
      $_ = /w/ ? lc reverse : $_ for @words;
      shift @caps and $_ = ucfirst for @words;
      return join "", @words


      use Test::More tests => 1;
      my $r = rev('Hello, my name is Yimin Rong.');
      is $r, 'Olleh, ym eman si Nimiy Gnor.';






      share|improve this answer













      share|improve this answer



      share|improve this answer











      answered Jul 18 at 13:31









      choroba

      24.1k33967




      24.1k33967











      • Thanks, but I used reverse just as an illustration. The actual utility I need to use is much more complex. Imagine reverse as a black box not easily duplicated in a scripting language.
        – Yimin Rong
        Jul 18 at 13:35










      • The trick is to write reverse in Perl, too ;-)
        – choroba
        Jul 18 at 13:42










      • Trust me, what I'm using isn't easy in perl, think about a factoring utility using the general number field sieve.
        – Yimin Rong
        Jul 18 at 13:46











      • @YiminRong: If it's easy in shell, it must be easy in Perl, too.
        – choroba
        Jul 18 at 14:11










      • Rewrote to illustrate with factor. Good luck writing that in perl!
        – Yimin Rong
        Jul 18 at 15:06
















      • Thanks, but I used reverse just as an illustration. The actual utility I need to use is much more complex. Imagine reverse as a black box not easily duplicated in a scripting language.
        – Yimin Rong
        Jul 18 at 13:35










      • The trick is to write reverse in Perl, too ;-)
        – choroba
        Jul 18 at 13:42










      • Trust me, what I'm using isn't easy in perl, think about a factoring utility using the general number field sieve.
        – Yimin Rong
        Jul 18 at 13:46











      • @YiminRong: If it's easy in shell, it must be easy in Perl, too.
        – choroba
        Jul 18 at 14:11










      • Rewrote to illustrate with factor. Good luck writing that in perl!
        – Yimin Rong
        Jul 18 at 15:06















      Thanks, but I used reverse just as an illustration. The actual utility I need to use is much more complex. Imagine reverse as a black box not easily duplicated in a scripting language.
      – Yimin Rong
      Jul 18 at 13:35




      Thanks, but I used reverse just as an illustration. The actual utility I need to use is much more complex. Imagine reverse as a black box not easily duplicated in a scripting language.
      – Yimin Rong
      Jul 18 at 13:35












      The trick is to write reverse in Perl, too ;-)
      – choroba
      Jul 18 at 13:42




      The trick is to write reverse in Perl, too ;-)
      – choroba
      Jul 18 at 13:42












      Trust me, what I'm using isn't easy in perl, think about a factoring utility using the general number field sieve.
      – Yimin Rong
      Jul 18 at 13:46





      Trust me, what I'm using isn't easy in perl, think about a factoring utility using the general number field sieve.
      – Yimin Rong
      Jul 18 at 13:46













      @YiminRong: If it's easy in shell, it must be easy in Perl, too.
      – choroba
      Jul 18 at 14:11




      @YiminRong: If it's easy in shell, it must be easy in Perl, too.
      – choroba
      Jul 18 at 14:11












      Rewrote to illustrate with factor. Good luck writing that in perl!
      – Yimin Rong
      Jul 18 at 15:06




      Rewrote to illustrate with factor. Good luck writing that in perl!
      – Yimin Rong
      Jul 18 at 15:06












      up vote
      0
      down vote













      Try the below awk solution:



      echo "Hello" | awk ' for ( i=length($0);i>0;i-- ) if (i == length($0) ) printf "%s",toupper(substr($0,i,1)) else printf "%s",tolower(substr($0,i,1)) printf "n" '


      Take each letter of the word character by character stating from the last. If it is the last character, convert to upper case and print otherwise print lower case.






      share|improve this answer

























        up vote
        0
        down vote













        Try the below awk solution:



        echo "Hello" | awk ' for ( i=length($0);i>0;i-- ) if (i == length($0) ) printf "%s",toupper(substr($0,i,1)) else printf "%s",tolower(substr($0,i,1)) printf "n" '


        Take each letter of the word character by character stating from the last. If it is the last character, convert to upper case and print otherwise print lower case.






        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          Try the below awk solution:



          echo "Hello" | awk ' for ( i=length($0);i>0;i-- ) if (i == length($0) ) printf "%s",toupper(substr($0,i,1)) else printf "%s",tolower(substr($0,i,1)) printf "n" '


          Take each letter of the word character by character stating from the last. If it is the last character, convert to upper case and print otherwise print lower case.






          share|improve this answer













          Try the below awk solution:



          echo "Hello" | awk ' for ( i=length($0);i>0;i-- ) if (i == length($0) ) printf "%s",toupper(substr($0,i,1)) else printf "%s",tolower(substr($0,i,1)) printf "n" '


          Take each letter of the word character by character stating from the last. If it is the last character, convert to upper case and print otherwise print lower case.







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Jul 18 at 13:57









          Raman Sailopal

          1,16317




          1,16317




















              up vote
              0
              down vote













              Run the following :



               perl -pe 's/(d+)/qx(factor $1) =~ s|n||r/ge' input-file.txt


              The reason why your sed command is not working is because sed will execute the entire pattern space unlike Perl which is aligned with how you were thinking and executes just as much as is on the rhs of the s/// command and replaces tonly that much with the command output.



              And that's precisely why if you notice sed was cribbing abt "Given:" utility not found. Given was the beginning of your pattern space. HTH






              share|improve this answer



























                up vote
                0
                down vote













                Run the following :



                 perl -pe 's/(d+)/qx(factor $1) =~ s|n||r/ge' input-file.txt


                The reason why your sed command is not working is because sed will execute the entire pattern space unlike Perl which is aligned with how you were thinking and executes just as much as is on the rhs of the s/// command and replaces tonly that much with the command output.



                And that's precisely why if you notice sed was cribbing abt "Given:" utility not found. Given was the beginning of your pattern space. HTH






                share|improve this answer

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Run the following :



                   perl -pe 's/(d+)/qx(factor $1) =~ s|n||r/ge' input-file.txt


                  The reason why your sed command is not working is because sed will execute the entire pattern space unlike Perl which is aligned with how you were thinking and executes just as much as is on the rhs of the s/// command and replaces tonly that much with the command output.



                  And that's precisely why if you notice sed was cribbing abt "Given:" utility not found. Given was the beginning of your pattern space. HTH






                  share|improve this answer















                  Run the following :



                   perl -pe 's/(d+)/qx(factor $1) =~ s|n||r/ge' input-file.txt


                  The reason why your sed command is not working is because sed will execute the entire pattern space unlike Perl which is aligned with how you were thinking and executes just as much as is on the rhs of the s/// command and replaces tonly that much with the command output.



                  And that's precisely why if you notice sed was cribbing abt "Given:" utility not found. Given was the beginning of your pattern space. HTH







                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited Jul 20 at 18:48


























                  answered Jul 20 at 18:09









                  Rakesh Sharma

                  11013




                  11013




















                      up vote
                      -1
                      down vote













                      Run the following:



                      perl -pe 's/(d+)/qx(factor $1) =~ s|n||r/ge' input-file.txt





                      share|improve this answer























                      • Care to explain how this works?
                        – Kusalananda
                        Jul 20 at 19:08














                      up vote
                      -1
                      down vote













                      Run the following:



                      perl -pe 's/(d+)/qx(factor $1) =~ s|n||r/ge' input-file.txt





                      share|improve this answer























                      • Care to explain how this works?
                        – Kusalananda
                        Jul 20 at 19:08












                      up vote
                      -1
                      down vote










                      up vote
                      -1
                      down vote









                      Run the following:



                      perl -pe 's/(d+)/qx(factor $1) =~ s|n||r/ge' input-file.txt





                      share|improve this answer















                      Run the following:



                      perl -pe 's/(d+)/qx(factor $1) =~ s|n||r/ge' input-file.txt






                      share|improve this answer















                      share|improve this answer



                      share|improve this answer








                      edited Jul 20 at 19:43









                      Jeff Schaller

                      30.8k846104




                      30.8k846104











                      answered Jul 20 at 18:07









                      Rakesh Sharma

                      11013




                      11013











                      • Care to explain how this works?
                        – Kusalananda
                        Jul 20 at 19:08
















                      • Care to explain how this works?
                        – Kusalananda
                        Jul 20 at 19:08















                      Care to explain how this works?
                      – Kusalananda
                      Jul 20 at 19:08




                      Care to explain how this works?
                      – Kusalananda
                      Jul 20 at 19:08












                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f456991%2fsearch-and-replace-using-a-custom-utility%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay