How to subtract from numbers following a string in VIM?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am trying to subtract from numbers following a string in VIM, for example this (searched) input:



CustomModelData: 1



And my preferred (replaced) output:



CustomModelData: -2147483647



The number that I am trying to subtract from the numbers is 2147483648. I tried using this in VIM, but it doesn't work since there is a string in the expression, I want to keep the string while also subtracting from the number that follows:



:%s/CustomModelData: d+/=/CustomModelData: submatch(0)-2147483648/g










share|improve this question























  • Do you have to use vim for this problem? Tools such as awk can do a much better job: awk -F ': ' 'BEGIN { OFS=": "} {print $1, $2 - 2147483648}' data-file

    – Rico Chen
    Nov 17 '18 at 5:14











  • No, I never considered using awk, I will try it out.

    – Mario
    Nov 17 '18 at 5:17













  • I tried entering the command and I got this error: awk: cmd. line:1: BEGIN { OFS=: awk: cmd. line:1: ^ syntax error errcount: 1

    – Mario
    Nov 17 '18 at 5:23













  • Can you try awk -F ': ' 'BEGIN {OFS=FS} {print $1, $2 - 2147483648}' data-file

    – Rico Chen
    Nov 17 '18 at 5:46











  • It seems to work, but it doesn't seem to go throughout the file (it only does the first one), and also, I had to add the "CustomModelData" before the first colon to make it work.

    – Mario
    Nov 17 '18 at 5:49


















1















I am trying to subtract from numbers following a string in VIM, for example this (searched) input:



CustomModelData: 1



And my preferred (replaced) output:



CustomModelData: -2147483647



The number that I am trying to subtract from the numbers is 2147483648. I tried using this in VIM, but it doesn't work since there is a string in the expression, I want to keep the string while also subtracting from the number that follows:



:%s/CustomModelData: d+/=/CustomModelData: submatch(0)-2147483648/g










share|improve this question























  • Do you have to use vim for this problem? Tools such as awk can do a much better job: awk -F ': ' 'BEGIN { OFS=": "} {print $1, $2 - 2147483648}' data-file

    – Rico Chen
    Nov 17 '18 at 5:14











  • No, I never considered using awk, I will try it out.

    – Mario
    Nov 17 '18 at 5:17













  • I tried entering the command and I got this error: awk: cmd. line:1: BEGIN { OFS=: awk: cmd. line:1: ^ syntax error errcount: 1

    – Mario
    Nov 17 '18 at 5:23













  • Can you try awk -F ': ' 'BEGIN {OFS=FS} {print $1, $2 - 2147483648}' data-file

    – Rico Chen
    Nov 17 '18 at 5:46











  • It seems to work, but it doesn't seem to go throughout the file (it only does the first one), and also, I had to add the "CustomModelData" before the first colon to make it work.

    – Mario
    Nov 17 '18 at 5:49














1












1








1








I am trying to subtract from numbers following a string in VIM, for example this (searched) input:



CustomModelData: 1



And my preferred (replaced) output:



CustomModelData: -2147483647



The number that I am trying to subtract from the numbers is 2147483648. I tried using this in VIM, but it doesn't work since there is a string in the expression, I want to keep the string while also subtracting from the number that follows:



:%s/CustomModelData: d+/=/CustomModelData: submatch(0)-2147483648/g










share|improve this question














I am trying to subtract from numbers following a string in VIM, for example this (searched) input:



CustomModelData: 1



And my preferred (replaced) output:



CustomModelData: -2147483647



The number that I am trying to subtract from the numbers is 2147483648. I tried using this in VIM, but it doesn't work since there is a string in the expression, I want to keep the string while also subtracting from the number that follows:



:%s/CustomModelData: d+/=/CustomModelData: submatch(0)-2147483648/g







vim






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 17 '18 at 4:47









MarioMario

82




82













  • Do you have to use vim for this problem? Tools such as awk can do a much better job: awk -F ': ' 'BEGIN { OFS=": "} {print $1, $2 - 2147483648}' data-file

    – Rico Chen
    Nov 17 '18 at 5:14











  • No, I never considered using awk, I will try it out.

    – Mario
    Nov 17 '18 at 5:17













  • I tried entering the command and I got this error: awk: cmd. line:1: BEGIN { OFS=: awk: cmd. line:1: ^ syntax error errcount: 1

    – Mario
    Nov 17 '18 at 5:23













  • Can you try awk -F ': ' 'BEGIN {OFS=FS} {print $1, $2 - 2147483648}' data-file

    – Rico Chen
    Nov 17 '18 at 5:46











  • It seems to work, but it doesn't seem to go throughout the file (it only does the first one), and also, I had to add the "CustomModelData" before the first colon to make it work.

    – Mario
    Nov 17 '18 at 5:49



















  • Do you have to use vim for this problem? Tools such as awk can do a much better job: awk -F ': ' 'BEGIN { OFS=": "} {print $1, $2 - 2147483648}' data-file

    – Rico Chen
    Nov 17 '18 at 5:14











  • No, I never considered using awk, I will try it out.

    – Mario
    Nov 17 '18 at 5:17













  • I tried entering the command and I got this error: awk: cmd. line:1: BEGIN { OFS=: awk: cmd. line:1: ^ syntax error errcount: 1

    – Mario
    Nov 17 '18 at 5:23













  • Can you try awk -F ': ' 'BEGIN {OFS=FS} {print $1, $2 - 2147483648}' data-file

    – Rico Chen
    Nov 17 '18 at 5:46











  • It seems to work, but it doesn't seem to go throughout the file (it only does the first one), and also, I had to add the "CustomModelData" before the first colon to make it work.

    – Mario
    Nov 17 '18 at 5:49

















Do you have to use vim for this problem? Tools such as awk can do a much better job: awk -F ': ' 'BEGIN { OFS=": "} {print $1, $2 - 2147483648}' data-file

– Rico Chen
Nov 17 '18 at 5:14





Do you have to use vim for this problem? Tools such as awk can do a much better job: awk -F ': ' 'BEGIN { OFS=": "} {print $1, $2 - 2147483648}' data-file

– Rico Chen
Nov 17 '18 at 5:14













No, I never considered using awk, I will try it out.

– Mario
Nov 17 '18 at 5:17







No, I never considered using awk, I will try it out.

– Mario
Nov 17 '18 at 5:17















I tried entering the command and I got this error: awk: cmd. line:1: BEGIN { OFS=: awk: cmd. line:1: ^ syntax error errcount: 1

– Mario
Nov 17 '18 at 5:23







I tried entering the command and I got this error: awk: cmd. line:1: BEGIN { OFS=: awk: cmd. line:1: ^ syntax error errcount: 1

– Mario
Nov 17 '18 at 5:23















Can you try awk -F ': ' 'BEGIN {OFS=FS} {print $1, $2 - 2147483648}' data-file

– Rico Chen
Nov 17 '18 at 5:46





Can you try awk -F ': ' 'BEGIN {OFS=FS} {print $1, $2 - 2147483648}' data-file

– Rico Chen
Nov 17 '18 at 5:46













It seems to work, but it doesn't seem to go throughout the file (it only does the first one), and also, I had to add the "CustomModelData" before the first colon to make it work.

– Mario
Nov 17 '18 at 5:49





It seems to work, but it doesn't seem to go throughout the file (it only does the first one), and also, I had to add the "CustomModelData" before the first colon to make it work.

– Mario
Nov 17 '18 at 5:49












2 Answers
2






active

oldest

votes


















2














You can use following minor adjustment to have the match start at the number so the replacement only sees the number to adjust



%s/vCustomModelData: zsd+/=submatch(0)-2147483648/g



  • zs anything, sets start of match






share|improve this answer































    0














    If each number is on a separate line of the file, then you can match relevant lines and use the Ctrl+X subtraction operator on them:



    :g/CustomModelData:/ norm 2147483648^X


    (For the ^X, type Ctrl+X.)






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348313%2fhow-to-subtract-from-numbers-following-a-string-in-vim%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You can use following minor adjustment to have the match start at the number so the replacement only sees the number to adjust



      %s/vCustomModelData: zsd+/=submatch(0)-2147483648/g



      • zs anything, sets start of match






      share|improve this answer




























        2














        You can use following minor adjustment to have the match start at the number so the replacement only sees the number to adjust



        %s/vCustomModelData: zsd+/=submatch(0)-2147483648/g



        • zs anything, sets start of match






        share|improve this answer


























          2












          2








          2







          You can use following minor adjustment to have the match start at the number so the replacement only sees the number to adjust



          %s/vCustomModelData: zsd+/=submatch(0)-2147483648/g



          • zs anything, sets start of match






          share|improve this answer













          You can use following minor adjustment to have the match start at the number so the replacement only sees the number to adjust



          %s/vCustomModelData: zsd+/=submatch(0)-2147483648/g



          • zs anything, sets start of match







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 17 '18 at 10:06









          Lieven KeersmaekersLieven Keersmaekers

          48k1190127




          48k1190127

























              0














              If each number is on a separate line of the file, then you can match relevant lines and use the Ctrl+X subtraction operator on them:



              :g/CustomModelData:/ norm 2147483648^X


              (For the ^X, type Ctrl+X.)






              share|improve this answer




























                0














                If each number is on a separate line of the file, then you can match relevant lines and use the Ctrl+X subtraction operator on them:



                :g/CustomModelData:/ norm 2147483648^X


                (For the ^X, type Ctrl+X.)






                share|improve this answer


























                  0












                  0








                  0







                  If each number is on a separate line of the file, then you can match relevant lines and use the Ctrl+X subtraction operator on them:



                  :g/CustomModelData:/ norm 2147483648^X


                  (For the ^X, type Ctrl+X.)






                  share|improve this answer













                  If each number is on a separate line of the file, then you can match relevant lines and use the Ctrl+X subtraction operator on them:



                  :g/CustomModelData:/ norm 2147483648^X


                  (For the ^X, type Ctrl+X.)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 9:42









                  SmylersSmylers

                  1,3901012




                  1,3901012






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348313%2fhow-to-subtract-from-numbers-following-a-string-in-vim%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Bressuire

                      Vorschmack

                      Quarantine