How to keep record of previous operation results in awk? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionReplace matches with multiline string using sedHow to use AWK record separator?How to use ^#$ as record separator in awk?How do I keep awk variables in scope?Awk hangs during math operation on Solarisawk - skip first line of recordI want to drop the record base on previous record using awk?parameter operation inside awk lineConvert record based (multiline) text to csv with awkAwk: setting a record to a pattern match, then print only the last record

Is the address of a local variable a constexpr?

Disable hyphenation for an entire paragraph

Why does Python start at index -1 when indexing a list from the end?

What do you call a plan that's an alternative plan in case your initial plan fails?

Should I discuss the type of campaign with my players?

Is 1 ppb equal to 1 μg/kg?

Does accepting a pardon have any bearing on trying that person for the same crime in a sovereign jurisdiction?

Models of set theory where not every set can be linearly ordered

List *all* the tuples!

What is the correct way to use the pinch test for dehydration?

How to recreate this effect in Photoshop?

Is it ethical to give a final exam after the professor has quit before teaching the remaining chapters of the course?

Determinant is linear as a function of each of the rows of the matrix.

Right-skewed distribution with mean equals to mode?

If 'B is more likely given A', then 'A is more likely given B'

How to deal with a team lead who never gives me credit?

3 doors, three guards, one stone

Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?

Sorting numerically

Is there a documented rationale why the House Ways and Means chairman can demand tax info?

How can I fade player when goes inside or outside of the area?

Are my PIs rude or am I just being too sensitive?

Why is "Captain Marvel" translated as male in Portugal?

What do you call a phrase that's not an idiom yet?



How to keep record of previous operation results in awk?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionReplace matches with multiline string using sedHow to use AWK record separator?How to use ^#$ as record separator in awk?How do I keep awk variables in scope?Awk hangs during math operation on Solarisawk - skip first line of recordI want to drop the record base on previous record using awk?parameter operation inside awk lineConvert record based (multiline) text to csv with awkAwk: setting a record to a pattern match, then print only the last record



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








4















I have a file filled with data in the following format. There are 3 lines that need to stay together and they have a predictable pattern:



dn: uid=N-NAME-02, ou=data01, dc=data02, dc=data03
uidNumber: 3423
sambaSID: S-1-1-11-1111111-111111111-11111111-12342
<blank line>
dn: uid=N-NAME-03, ou=data01, dc=data02, dc=data03
uidNumber: 3245
sambaSID: S-1-1-11-1111111-111111111-11111111-32212


I need to do some math based the second string of data in the group of 3 and then place the resulting calculation into the third string of the group:



dn: uid=NAME02, ou=data01, dc=data02, dc=data03
uidNumber: (3423 + 2 * 100)
sambaSID: S-1-1-11-1111111-111111111-11111111-342500
<blank line>
dn: uid=NAME03, ou=data01, dc=data02, dc=data03
uidNumber: (3245 + 2 * 100)
sambaSID: S-1-1-11-1111111-111111111-11111111-324700


I figured I could again use AWK for this since AWK can do the math and correct placement of the resulting value.
I got the math part on it's own and stored it in a variable



variable1=`awk -F ': ' '/uidNumber:/ new = $2 * 2 + 1000; print new ' infile`


I was then able to use this variable in another command on the same file to do the replace



awk -F '-' -v variable2=$variable1 '/pattern of string 3 / print $1"-"$2"-"$3"-"$4"-"$5"-"$6"-"$7"-"variable2 '


I should mention that the string 3 data is a long string separated by dashes '-'. After the very last dash is where the calculated value needs to be placed.



This all works but has one major flaw... it only works if there is one record in the file.
Can someone maybe give me a hint as to a way to accomplish this?










share|improve this question






























    4















    I have a file filled with data in the following format. There are 3 lines that need to stay together and they have a predictable pattern:



    dn: uid=N-NAME-02, ou=data01, dc=data02, dc=data03
    uidNumber: 3423
    sambaSID: S-1-1-11-1111111-111111111-11111111-12342
    <blank line>
    dn: uid=N-NAME-03, ou=data01, dc=data02, dc=data03
    uidNumber: 3245
    sambaSID: S-1-1-11-1111111-111111111-11111111-32212


    I need to do some math based the second string of data in the group of 3 and then place the resulting calculation into the third string of the group:



    dn: uid=NAME02, ou=data01, dc=data02, dc=data03
    uidNumber: (3423 + 2 * 100)
    sambaSID: S-1-1-11-1111111-111111111-11111111-342500
    <blank line>
    dn: uid=NAME03, ou=data01, dc=data02, dc=data03
    uidNumber: (3245 + 2 * 100)
    sambaSID: S-1-1-11-1111111-111111111-11111111-324700


    I figured I could again use AWK for this since AWK can do the math and correct placement of the resulting value.
    I got the math part on it's own and stored it in a variable



    variable1=`awk -F ': ' '/uidNumber:/ new = $2 * 2 + 1000; print new ' infile`


    I was then able to use this variable in another command on the same file to do the replace



    awk -F '-' -v variable2=$variable1 '/pattern of string 3 / print $1"-"$2"-"$3"-"$4"-"$5"-"$6"-"$7"-"variable2 '


    I should mention that the string 3 data is a long string separated by dashes '-'. After the very last dash is where the calculated value needs to be placed.



    This all works but has one major flaw... it only works if there is one record in the file.
    Can someone maybe give me a hint as to a way to accomplish this?










    share|improve this question


























      4












      4








      4








      I have a file filled with data in the following format. There are 3 lines that need to stay together and they have a predictable pattern:



      dn: uid=N-NAME-02, ou=data01, dc=data02, dc=data03
      uidNumber: 3423
      sambaSID: S-1-1-11-1111111-111111111-11111111-12342
      <blank line>
      dn: uid=N-NAME-03, ou=data01, dc=data02, dc=data03
      uidNumber: 3245
      sambaSID: S-1-1-11-1111111-111111111-11111111-32212


      I need to do some math based the second string of data in the group of 3 and then place the resulting calculation into the third string of the group:



      dn: uid=NAME02, ou=data01, dc=data02, dc=data03
      uidNumber: (3423 + 2 * 100)
      sambaSID: S-1-1-11-1111111-111111111-11111111-342500
      <blank line>
      dn: uid=NAME03, ou=data01, dc=data02, dc=data03
      uidNumber: (3245 + 2 * 100)
      sambaSID: S-1-1-11-1111111-111111111-11111111-324700


      I figured I could again use AWK for this since AWK can do the math and correct placement of the resulting value.
      I got the math part on it's own and stored it in a variable



      variable1=`awk -F ': ' '/uidNumber:/ new = $2 * 2 + 1000; print new ' infile`


      I was then able to use this variable in another command on the same file to do the replace



      awk -F '-' -v variable2=$variable1 '/pattern of string 3 / print $1"-"$2"-"$3"-"$4"-"$5"-"$6"-"$7"-"variable2 '


      I should mention that the string 3 data is a long string separated by dashes '-'. After the very last dash is where the calculated value needs to be placed.



      This all works but has one major flaw... it only works if there is one record in the file.
      Can someone maybe give me a hint as to a way to accomplish this?










      share|improve this question
















      I have a file filled with data in the following format. There are 3 lines that need to stay together and they have a predictable pattern:



      dn: uid=N-NAME-02, ou=data01, dc=data02, dc=data03
      uidNumber: 3423
      sambaSID: S-1-1-11-1111111-111111111-11111111-12342
      <blank line>
      dn: uid=N-NAME-03, ou=data01, dc=data02, dc=data03
      uidNumber: 3245
      sambaSID: S-1-1-11-1111111-111111111-11111111-32212


      I need to do some math based the second string of data in the group of 3 and then place the resulting calculation into the third string of the group:



      dn: uid=NAME02, ou=data01, dc=data02, dc=data03
      uidNumber: (3423 + 2 * 100)
      sambaSID: S-1-1-11-1111111-111111111-11111111-342500
      <blank line>
      dn: uid=NAME03, ou=data01, dc=data02, dc=data03
      uidNumber: (3245 + 2 * 100)
      sambaSID: S-1-1-11-1111111-111111111-11111111-324700


      I figured I could again use AWK for this since AWK can do the math and correct placement of the resulting value.
      I got the math part on it's own and stored it in a variable



      variable1=`awk -F ': ' '/uidNumber:/ new = $2 * 2 + 1000; print new ' infile`


      I was then able to use this variable in another command on the same file to do the replace



      awk -F '-' -v variable2=$variable1 '/pattern of string 3 / print $1"-"$2"-"$3"-"$4"-"$5"-"$6"-"$7"-"variable2 '


      I should mention that the string 3 data is a long string separated by dashes '-'. After the very last dash is where the calculated value needs to be placed.



      This all works but has one major flaw... it only works if there is one record in the file.
      Can someone maybe give me a hint as to a way to accomplish this?







      awk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 11 hours ago









      Rui F Ribeiro

      42.1k1483142




      42.1k1483142










      asked May 28 '14 at 20:03









      bournebourne

      82129




      82129




















          3 Answers
          3






          active

          oldest

          votes


















          3














          You can use the match() function in awk:



          $ cat file
          somedata45
          somedata47
          somedata67

          somedata53
          somedata23
          somedata12



          awk '
          BEGIN RS = ""; OFS = "n"; ORS = "nn"
          match($2, /[0-9]+/) value = (substr($2, RSTART, RLENGTH) + 5) * 100
          match($3, /[0-9]+/) $3 = substr($2, 1, RSTART - 1) value 1' file
          somedata45
          somedata47
          somedata5200

          somedata53
          somedata23
          somedata2800


          We set the Record Separator to nothing effectively enabling the paragraph mode (separated by blank line). The second line in each paragraph becomes our $2, third line becomes $3 etc. We set the Output Field Separator to newline. Due to the paragraph mode, we also set Output Record Separator to two newlines. The output will give you an extra newline at the end.



          We use the match() function to identify the start of number. When a match is found, the function populates two variables for us, RSTART and RLENGTH indicating when the match starts and how long it is. We use those variables to do our calculation and store the result in variable called value. We use the substr function to locate the numbers.



          We repeat the same for $3 and this time we use substr function to print up to where our numbers start and replace the number piece with our variable that contains the calculated value from previous line.



          Please refer the String Functions in the user guide for more details.




          Update based on real data:



          Your real data actually makes it a lot simpler.



          awk '
          /^uidNumber/ value = $NF
          /^sambaSID/
          n = split ($NF, tmp, /-/)
          tmp[n] = ((value + 2)* 100)
          for (i=1; i<=n; i++) nf = (nf ? nf "-" tmp[i] : tmp[i])
          $NF = nf
          nf = ""
          1' file
          dn: uid=NAME02, ou=data01, dc=data02, dc=data03
          uidNumber: 3423
          sambaSID: S-1-1-11-1111111-111111111-11111111-342500

          dn: uid=NAME03, ou=data01, dc=data02, dc=data03
          uidNumber: 3245
          sambaSID: S-1-1-11-1111111-111111111-11111111-324700


          You look for the line with uidNumber and capture the last field. When you see a line with sambaSID you split the last field on - and modify the last element to your new calculated value. You then use a for loop to re-assemble your last field.






          share|improve this answer
































            0














            The way I would do this sort of thing in an awk script is by keeping some variable to count the lines. You can do this in a single script.



            /^[ t]*$/ lineCnt = 0; continue # this matches blank lines and resets the count
            lineCnt++
            lineCnt == 2 ... strip off your numeric ...
            lineCnt == 3 ... do whatever you need to with the math ...


            You could make it slightly more efficient by adding 'continue' statements at the end of the blocks for lines 2 and 3, since you know you are done.






            share|improve this answer

























            • Thank you! counting of the lines makes sense and I was wondering if there was a way to do this. I will work on your suggestion and see if I can make it work!

              – bourne
              May 29 '14 at 12:40






            • 1





              One thing to be aware of - variables are untyped and do not need to be declared in advance of use. At first use they have the value of empty when used as string and zero when used as int. I wasn't sure of this myself so tried it out before posting. You can declare a BEGIN block if variables need initialization to non-default values.

              – BobDoolittle
              May 29 '14 at 21:29


















            0














            It sounds like you're just asking how to combine multiple pattern-action expressions in awk, using the values of variables from one in another - which is very simple e.g.



            awk '/pattern2/ calculate and assign var ; /pattern3/ do something with var'


            The ; is not strictly necessary in this case but helps comprehension. In an awk script (as opposed to than a one-liner) you'd probably separate the expressions with newlines.



            For example, if file is



            pattern1
            pattern2 3 5
            pattern3


            then



            $ awk '/pattern2/ var = $2+$3; /pattern3/ print; print var' file
            pattern3
            8


            Also, you may wish to take a look at awk's builtin OFS (output field separator) variable, which lets you format the output more easily e.g. given



            pattern1
            pattern2-3-5
            pattern3-4-6


            then



            $ awk -F- '/pattern2/ var = $2+$3; /pattern3/ OFS="-"; print $1,$2,$3,var' file
            pattern3-4-6-8





            share|improve this answer

























            • Thank you very much. When I started working with this solution I realized that the way I represented my data was bad. I think this would work perfectly if I wasn't dealing with multiple different possible delimiters. I will continue to tinker with it!

              – bourne
              May 29 '14 at 12:44











            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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2funix.stackexchange.com%2fquestions%2f132436%2fhow-to-keep-record-of-previous-operation-results-in-awk%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            You can use the match() function in awk:



            $ cat file
            somedata45
            somedata47
            somedata67

            somedata53
            somedata23
            somedata12



            awk '
            BEGIN RS = ""; OFS = "n"; ORS = "nn"
            match($2, /[0-9]+/) value = (substr($2, RSTART, RLENGTH) + 5) * 100
            match($3, /[0-9]+/) $3 = substr($2, 1, RSTART - 1) value 1' file
            somedata45
            somedata47
            somedata5200

            somedata53
            somedata23
            somedata2800


            We set the Record Separator to nothing effectively enabling the paragraph mode (separated by blank line). The second line in each paragraph becomes our $2, third line becomes $3 etc. We set the Output Field Separator to newline. Due to the paragraph mode, we also set Output Record Separator to two newlines. The output will give you an extra newline at the end.



            We use the match() function to identify the start of number. When a match is found, the function populates two variables for us, RSTART and RLENGTH indicating when the match starts and how long it is. We use those variables to do our calculation and store the result in variable called value. We use the substr function to locate the numbers.



            We repeat the same for $3 and this time we use substr function to print up to where our numbers start and replace the number piece with our variable that contains the calculated value from previous line.



            Please refer the String Functions in the user guide for more details.




            Update based on real data:



            Your real data actually makes it a lot simpler.



            awk '
            /^uidNumber/ value = $NF
            /^sambaSID/
            n = split ($NF, tmp, /-/)
            tmp[n] = ((value + 2)* 100)
            for (i=1; i<=n; i++) nf = (nf ? nf "-" tmp[i] : tmp[i])
            $NF = nf
            nf = ""
            1' file
            dn: uid=NAME02, ou=data01, dc=data02, dc=data03
            uidNumber: 3423
            sambaSID: S-1-1-11-1111111-111111111-11111111-342500

            dn: uid=NAME03, ou=data01, dc=data02, dc=data03
            uidNumber: 3245
            sambaSID: S-1-1-11-1111111-111111111-11111111-324700


            You look for the line with uidNumber and capture the last field. When you see a line with sambaSID you split the last field on - and modify the last element to your new calculated value. You then use a for loop to re-assemble your last field.






            share|improve this answer





























              3














              You can use the match() function in awk:



              $ cat file
              somedata45
              somedata47
              somedata67

              somedata53
              somedata23
              somedata12



              awk '
              BEGIN RS = ""; OFS = "n"; ORS = "nn"
              match($2, /[0-9]+/) value = (substr($2, RSTART, RLENGTH) + 5) * 100
              match($3, /[0-9]+/) $3 = substr($2, 1, RSTART - 1) value 1' file
              somedata45
              somedata47
              somedata5200

              somedata53
              somedata23
              somedata2800


              We set the Record Separator to nothing effectively enabling the paragraph mode (separated by blank line). The second line in each paragraph becomes our $2, third line becomes $3 etc. We set the Output Field Separator to newline. Due to the paragraph mode, we also set Output Record Separator to two newlines. The output will give you an extra newline at the end.



              We use the match() function to identify the start of number. When a match is found, the function populates two variables for us, RSTART and RLENGTH indicating when the match starts and how long it is. We use those variables to do our calculation and store the result in variable called value. We use the substr function to locate the numbers.



              We repeat the same for $3 and this time we use substr function to print up to where our numbers start and replace the number piece with our variable that contains the calculated value from previous line.



              Please refer the String Functions in the user guide for more details.




              Update based on real data:



              Your real data actually makes it a lot simpler.



              awk '
              /^uidNumber/ value = $NF
              /^sambaSID/
              n = split ($NF, tmp, /-/)
              tmp[n] = ((value + 2)* 100)
              for (i=1; i<=n; i++) nf = (nf ? nf "-" tmp[i] : tmp[i])
              $NF = nf
              nf = ""
              1' file
              dn: uid=NAME02, ou=data01, dc=data02, dc=data03
              uidNumber: 3423
              sambaSID: S-1-1-11-1111111-111111111-11111111-342500

              dn: uid=NAME03, ou=data01, dc=data02, dc=data03
              uidNumber: 3245
              sambaSID: S-1-1-11-1111111-111111111-11111111-324700


              You look for the line with uidNumber and capture the last field. When you see a line with sambaSID you split the last field on - and modify the last element to your new calculated value. You then use a for loop to re-assemble your last field.






              share|improve this answer



























                3












                3








                3







                You can use the match() function in awk:



                $ cat file
                somedata45
                somedata47
                somedata67

                somedata53
                somedata23
                somedata12



                awk '
                BEGIN RS = ""; OFS = "n"; ORS = "nn"
                match($2, /[0-9]+/) value = (substr($2, RSTART, RLENGTH) + 5) * 100
                match($3, /[0-9]+/) $3 = substr($2, 1, RSTART - 1) value 1' file
                somedata45
                somedata47
                somedata5200

                somedata53
                somedata23
                somedata2800


                We set the Record Separator to nothing effectively enabling the paragraph mode (separated by blank line). The second line in each paragraph becomes our $2, third line becomes $3 etc. We set the Output Field Separator to newline. Due to the paragraph mode, we also set Output Record Separator to two newlines. The output will give you an extra newline at the end.



                We use the match() function to identify the start of number. When a match is found, the function populates two variables for us, RSTART and RLENGTH indicating when the match starts and how long it is. We use those variables to do our calculation and store the result in variable called value. We use the substr function to locate the numbers.



                We repeat the same for $3 and this time we use substr function to print up to where our numbers start and replace the number piece with our variable that contains the calculated value from previous line.



                Please refer the String Functions in the user guide for more details.




                Update based on real data:



                Your real data actually makes it a lot simpler.



                awk '
                /^uidNumber/ value = $NF
                /^sambaSID/
                n = split ($NF, tmp, /-/)
                tmp[n] = ((value + 2)* 100)
                for (i=1; i<=n; i++) nf = (nf ? nf "-" tmp[i] : tmp[i])
                $NF = nf
                nf = ""
                1' file
                dn: uid=NAME02, ou=data01, dc=data02, dc=data03
                uidNumber: 3423
                sambaSID: S-1-1-11-1111111-111111111-11111111-342500

                dn: uid=NAME03, ou=data01, dc=data02, dc=data03
                uidNumber: 3245
                sambaSID: S-1-1-11-1111111-111111111-11111111-324700


                You look for the line with uidNumber and capture the last field. When you see a line with sambaSID you split the last field on - and modify the last element to your new calculated value. You then use a for loop to re-assemble your last field.






                share|improve this answer















                You can use the match() function in awk:



                $ cat file
                somedata45
                somedata47
                somedata67

                somedata53
                somedata23
                somedata12



                awk '
                BEGIN RS = ""; OFS = "n"; ORS = "nn"
                match($2, /[0-9]+/) value = (substr($2, RSTART, RLENGTH) + 5) * 100
                match($3, /[0-9]+/) $3 = substr($2, 1, RSTART - 1) value 1' file
                somedata45
                somedata47
                somedata5200

                somedata53
                somedata23
                somedata2800


                We set the Record Separator to nothing effectively enabling the paragraph mode (separated by blank line). The second line in each paragraph becomes our $2, third line becomes $3 etc. We set the Output Field Separator to newline. Due to the paragraph mode, we also set Output Record Separator to two newlines. The output will give you an extra newline at the end.



                We use the match() function to identify the start of number. When a match is found, the function populates two variables for us, RSTART and RLENGTH indicating when the match starts and how long it is. We use those variables to do our calculation and store the result in variable called value. We use the substr function to locate the numbers.



                We repeat the same for $3 and this time we use substr function to print up to where our numbers start and replace the number piece with our variable that contains the calculated value from previous line.



                Please refer the String Functions in the user guide for more details.




                Update based on real data:



                Your real data actually makes it a lot simpler.



                awk '
                /^uidNumber/ value = $NF
                /^sambaSID/
                n = split ($NF, tmp, /-/)
                tmp[n] = ((value + 2)* 100)
                for (i=1; i<=n; i++) nf = (nf ? nf "-" tmp[i] : tmp[i])
                $NF = nf
                nf = ""
                1' file
                dn: uid=NAME02, ou=data01, dc=data02, dc=data03
                uidNumber: 3423
                sambaSID: S-1-1-11-1111111-111111111-11111111-342500

                dn: uid=NAME03, ou=data01, dc=data02, dc=data03
                uidNumber: 3245
                sambaSID: S-1-1-11-1111111-111111111-11111111-324700


                You look for the line with uidNumber and capture the last field. When you see a line with sambaSID you split the last field on - and modify the last element to your new calculated value. You then use a for loop to re-assemble your last field.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited May 29 '14 at 17:50

























                answered May 28 '14 at 21:09









                jaypal singhjaypal singh

                1,19211017




                1,19211017























                    0














                    The way I would do this sort of thing in an awk script is by keeping some variable to count the lines. You can do this in a single script.



                    /^[ t]*$/ lineCnt = 0; continue # this matches blank lines and resets the count
                    lineCnt++
                    lineCnt == 2 ... strip off your numeric ...
                    lineCnt == 3 ... do whatever you need to with the math ...


                    You could make it slightly more efficient by adding 'continue' statements at the end of the blocks for lines 2 and 3, since you know you are done.






                    share|improve this answer

























                    • Thank you! counting of the lines makes sense and I was wondering if there was a way to do this. I will work on your suggestion and see if I can make it work!

                      – bourne
                      May 29 '14 at 12:40






                    • 1





                      One thing to be aware of - variables are untyped and do not need to be declared in advance of use. At first use they have the value of empty when used as string and zero when used as int. I wasn't sure of this myself so tried it out before posting. You can declare a BEGIN block if variables need initialization to non-default values.

                      – BobDoolittle
                      May 29 '14 at 21:29















                    0














                    The way I would do this sort of thing in an awk script is by keeping some variable to count the lines. You can do this in a single script.



                    /^[ t]*$/ lineCnt = 0; continue # this matches blank lines and resets the count
                    lineCnt++
                    lineCnt == 2 ... strip off your numeric ...
                    lineCnt == 3 ... do whatever you need to with the math ...


                    You could make it slightly more efficient by adding 'continue' statements at the end of the blocks for lines 2 and 3, since you know you are done.






                    share|improve this answer

























                    • Thank you! counting of the lines makes sense and I was wondering if there was a way to do this. I will work on your suggestion and see if I can make it work!

                      – bourne
                      May 29 '14 at 12:40






                    • 1





                      One thing to be aware of - variables are untyped and do not need to be declared in advance of use. At first use they have the value of empty when used as string and zero when used as int. I wasn't sure of this myself so tried it out before posting. You can declare a BEGIN block if variables need initialization to non-default values.

                      – BobDoolittle
                      May 29 '14 at 21:29













                    0












                    0








                    0







                    The way I would do this sort of thing in an awk script is by keeping some variable to count the lines. You can do this in a single script.



                    /^[ t]*$/ lineCnt = 0; continue # this matches blank lines and resets the count
                    lineCnt++
                    lineCnt == 2 ... strip off your numeric ...
                    lineCnt == 3 ... do whatever you need to with the math ...


                    You could make it slightly more efficient by adding 'continue' statements at the end of the blocks for lines 2 and 3, since you know you are done.






                    share|improve this answer















                    The way I would do this sort of thing in an awk script is by keeping some variable to count the lines. You can do this in a single script.



                    /^[ t]*$/ lineCnt = 0; continue # this matches blank lines and resets the count
                    lineCnt++
                    lineCnt == 2 ... strip off your numeric ...
                    lineCnt == 3 ... do whatever you need to with the math ...


                    You could make it slightly more efficient by adding 'continue' statements at the end of the blocks for lines 2 and 3, since you know you are done.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited May 28 '14 at 20:21

























                    answered May 28 '14 at 20:15









                    BobDoolittleBobDoolittle

                    1,1591024




                    1,1591024












                    • Thank you! counting of the lines makes sense and I was wondering if there was a way to do this. I will work on your suggestion and see if I can make it work!

                      – bourne
                      May 29 '14 at 12:40






                    • 1





                      One thing to be aware of - variables are untyped and do not need to be declared in advance of use. At first use they have the value of empty when used as string and zero when used as int. I wasn't sure of this myself so tried it out before posting. You can declare a BEGIN block if variables need initialization to non-default values.

                      – BobDoolittle
                      May 29 '14 at 21:29

















                    • Thank you! counting of the lines makes sense and I was wondering if there was a way to do this. I will work on your suggestion and see if I can make it work!

                      – bourne
                      May 29 '14 at 12:40






                    • 1





                      One thing to be aware of - variables are untyped and do not need to be declared in advance of use. At first use they have the value of empty when used as string and zero when used as int. I wasn't sure of this myself so tried it out before posting. You can declare a BEGIN block if variables need initialization to non-default values.

                      – BobDoolittle
                      May 29 '14 at 21:29
















                    Thank you! counting of the lines makes sense and I was wondering if there was a way to do this. I will work on your suggestion and see if I can make it work!

                    – bourne
                    May 29 '14 at 12:40





                    Thank you! counting of the lines makes sense and I was wondering if there was a way to do this. I will work on your suggestion and see if I can make it work!

                    – bourne
                    May 29 '14 at 12:40




                    1




                    1





                    One thing to be aware of - variables are untyped and do not need to be declared in advance of use. At first use they have the value of empty when used as string and zero when used as int. I wasn't sure of this myself so tried it out before posting. You can declare a BEGIN block if variables need initialization to non-default values.

                    – BobDoolittle
                    May 29 '14 at 21:29





                    One thing to be aware of - variables are untyped and do not need to be declared in advance of use. At first use they have the value of empty when used as string and zero when used as int. I wasn't sure of this myself so tried it out before posting. You can declare a BEGIN block if variables need initialization to non-default values.

                    – BobDoolittle
                    May 29 '14 at 21:29











                    0














                    It sounds like you're just asking how to combine multiple pattern-action expressions in awk, using the values of variables from one in another - which is very simple e.g.



                    awk '/pattern2/ calculate and assign var ; /pattern3/ do something with var'


                    The ; is not strictly necessary in this case but helps comprehension. In an awk script (as opposed to than a one-liner) you'd probably separate the expressions with newlines.



                    For example, if file is



                    pattern1
                    pattern2 3 5
                    pattern3


                    then



                    $ awk '/pattern2/ var = $2+$3; /pattern3/ print; print var' file
                    pattern3
                    8


                    Also, you may wish to take a look at awk's builtin OFS (output field separator) variable, which lets you format the output more easily e.g. given



                    pattern1
                    pattern2-3-5
                    pattern3-4-6


                    then



                    $ awk -F- '/pattern2/ var = $2+$3; /pattern3/ OFS="-"; print $1,$2,$3,var' file
                    pattern3-4-6-8





                    share|improve this answer

























                    • Thank you very much. When I started working with this solution I realized that the way I represented my data was bad. I think this would work perfectly if I wasn't dealing with multiple different possible delimiters. I will continue to tinker with it!

                      – bourne
                      May 29 '14 at 12:44















                    0














                    It sounds like you're just asking how to combine multiple pattern-action expressions in awk, using the values of variables from one in another - which is very simple e.g.



                    awk '/pattern2/ calculate and assign var ; /pattern3/ do something with var'


                    The ; is not strictly necessary in this case but helps comprehension. In an awk script (as opposed to than a one-liner) you'd probably separate the expressions with newlines.



                    For example, if file is



                    pattern1
                    pattern2 3 5
                    pattern3


                    then



                    $ awk '/pattern2/ var = $2+$3; /pattern3/ print; print var' file
                    pattern3
                    8


                    Also, you may wish to take a look at awk's builtin OFS (output field separator) variable, which lets you format the output more easily e.g. given



                    pattern1
                    pattern2-3-5
                    pattern3-4-6


                    then



                    $ awk -F- '/pattern2/ var = $2+$3; /pattern3/ OFS="-"; print $1,$2,$3,var' file
                    pattern3-4-6-8





                    share|improve this answer

























                    • Thank you very much. When I started working with this solution I realized that the way I represented my data was bad. I think this would work perfectly if I wasn't dealing with multiple different possible delimiters. I will continue to tinker with it!

                      – bourne
                      May 29 '14 at 12:44













                    0












                    0








                    0







                    It sounds like you're just asking how to combine multiple pattern-action expressions in awk, using the values of variables from one in another - which is very simple e.g.



                    awk '/pattern2/ calculate and assign var ; /pattern3/ do something with var'


                    The ; is not strictly necessary in this case but helps comprehension. In an awk script (as opposed to than a one-liner) you'd probably separate the expressions with newlines.



                    For example, if file is



                    pattern1
                    pattern2 3 5
                    pattern3


                    then



                    $ awk '/pattern2/ var = $2+$3; /pattern3/ print; print var' file
                    pattern3
                    8


                    Also, you may wish to take a look at awk's builtin OFS (output field separator) variable, which lets you format the output more easily e.g. given



                    pattern1
                    pattern2-3-5
                    pattern3-4-6


                    then



                    $ awk -F- '/pattern2/ var = $2+$3; /pattern3/ OFS="-"; print $1,$2,$3,var' file
                    pattern3-4-6-8





                    share|improve this answer















                    It sounds like you're just asking how to combine multiple pattern-action expressions in awk, using the values of variables from one in another - which is very simple e.g.



                    awk '/pattern2/ calculate and assign var ; /pattern3/ do something with var'


                    The ; is not strictly necessary in this case but helps comprehension. In an awk script (as opposed to than a one-liner) you'd probably separate the expressions with newlines.



                    For example, if file is



                    pattern1
                    pattern2 3 5
                    pattern3


                    then



                    $ awk '/pattern2/ var = $2+$3; /pattern3/ print; print var' file
                    pattern3
                    8


                    Also, you may wish to take a look at awk's builtin OFS (output field separator) variable, which lets you format the output more easily e.g. given



                    pattern1
                    pattern2-3-5
                    pattern3-4-6


                    then



                    $ awk -F- '/pattern2/ var = $2+$3; /pattern3/ OFS="-"; print $1,$2,$3,var' file
                    pattern3-4-6-8






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited May 28 '14 at 20:35

























                    answered May 28 '14 at 20:26









                    steeldriversteeldriver

                    37.8k45489




                    37.8k45489












                    • Thank you very much. When I started working with this solution I realized that the way I represented my data was bad. I think this would work perfectly if I wasn't dealing with multiple different possible delimiters. I will continue to tinker with it!

                      – bourne
                      May 29 '14 at 12:44

















                    • Thank you very much. When I started working with this solution I realized that the way I represented my data was bad. I think this would work perfectly if I wasn't dealing with multiple different possible delimiters. I will continue to tinker with it!

                      – bourne
                      May 29 '14 at 12:44
















                    Thank you very much. When I started working with this solution I realized that the way I represented my data was bad. I think this would work perfectly if I wasn't dealing with multiple different possible delimiters. I will continue to tinker with it!

                    – bourne
                    May 29 '14 at 12:44





                    Thank you very much. When I started working with this solution I realized that the way I represented my data was bad. I think this would work perfectly if I wasn't dealing with multiple different possible delimiters. I will continue to tinker with it!

                    – bourne
                    May 29 '14 at 12:44

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • 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%2funix.stackexchange.com%2fquestions%2f132436%2fhow-to-keep-record-of-previous-operation-results-in-awk%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







                    -awk

                    Popular posts from this blog

                    Word for a person who has no opinion about whether god existsWord for having a definite opinion while simultaneously withholding judgment?What's the opposite of “newcomer? Is ”veteran" OK?What do you call an “atheist” who might believe in an afterlife?What's a word for someone who wants to voice opinions but not have them challenged?Word for someone who dismisses contrary opinions as irrational?Somone who thinks they are overly special/out of the ordinaryIs there a word, phrase or idiom for “a person who is incapable of thinking about the future”?The belief that a god is human-likeA word for a non-famous person/thing you have heard a lot aboutAdjective for a person who enjoys taking care of their appearance

                    What was this official D&D 3.5e Lovecraft-flavored rulebook?What was this set of RPG tools called?As a first-time DM should I let my players play complex character classes and roles?Nymph's Kiss and the RelationshipWhat was the name of this Cleric Prestige Class that shapes metal with its bare hands?Are the 3.5e Dragonlance books third party or official works?What's up with the domain Vile Darkness?What was this 80s book about RPGs?What was the name of this Werewolf band?What book had Rituals to “upgrade” animal companions to keep them viable at higher levels?What was this RPG that had rules for player-owned businesses?

                    2017 IndyCar Series Contents Series news Teams and drivers Schedule Season summary Footnotes References External links Navigation menu"INDYCAR: Initial 2018 bodywork concepts unveiled"the original"IndyCar confirms switch to Performance Friction brakes in 2017""AJ Foyt Racing will switch to Chevy"the original"Carlos Munoz, Conor Daly will drive for AJ Foyt Racing""Zach Veach's Indy 500 Debut Confirmed with Foyt""No mass exodus from Honda after Ganassi switch""Ex-F1 driver Sato joins Andretti Autosport for 2017 IndyCar season""IndyCar's Ryan Hunter-Reay, sponsor DHL paired through 2020""hhgregg and Andretti Autosport announce partnership for key races in 2016""INDYCAR: Rossi re-signs with Andretti"the original"McLaren Formula 1 - Fernando Alonso to race at Indy 500 with McLaren, Honda and Andretti Autosport""Shank will finally take part in Indy 500 with Harvey, Andretti | MotorSportsTalk""Andretti adds Jack Harvey to Indy 500 field""Ganassi switches to Honda power for 2017""INDYCAR: Chilton returns to Ganassi"the original"IndyCar silly season: Who's going where in 2017?""INDYCAR: Kanaan, NTT Data return to Ganassi"the original"Kimball to remain at Ganassi for 2017""Coyne confirms Bourdais for 2017 IndyCar season""Davison to sub for Bourdais in Indy 500"the original"Gutierrez confirmed for Detroit IndyCar debut""Gutierrez returns with Coyne for rest of 2017 season""Vautier to drive for Coyne at Texas"the original"INDYCAR: Coyne confirms Jones for 2017"the original"Pippa Mann returns to Coyne for Indy 500""Karam, Dreyer & Reinbold teaming up again for Indianapolis 500""Pigot to return to Ed Carpenter Racing""Hildebrand confirmed as full-time Ed Carpenter driver""Veach to replace injured Hildebrand at Barber"the originalNew Team Harding Racing Enters Chaves for 101st Indianapolis 500"Juncos Racing Announces Entry in 101st Running of the Indianapolis 500 :: Juncos Racing""Juncos confirms Pigot for Indy 500""Saavedra confirmed in Juncos' second 500 entry"the original"Lazier confirms Indy 500 run after son's USF2000 debut"the original"Claman DeMelo to race for RLLR at Sonoma"the original"Rahal signs Servia and ace engineer for 2017""IndyCar: Aleshin returns with Schmidt"the original"Aleshin replaced by Saavedra for Toronto""Jack Harvey will pilot SPM No. 7 car at Watkins Glen, Sonoma""Jay Howard confirmed in Tony Stewart's supported SPM Indy entry""INDYCAR: Newgarden to wave the flag at Penske"the original"Pagenaud opts for No. 1 in 2017"the original"Penske confirms Newgarden for 2017""Montoya to stay with Team Penske in 2017""Target leaving IndyCar after 27 seasons with Chip Ganassi""Cavin: IndyCar could see complete driver/team shakeup in 2017""End of the road for KV Racing?""KV Racing confirms closure, equipment sold to Juncos""Juncos confirms IndyCar Series entry"the original"Juncos readies IndyCar program, aims for '17 500"the original"Harding Racing to add Texas, Pocono to schedule"the original"Sato signs with Andretti Autosport for 2017""INDYCAR: Aleshin in Doubt at SPM"the original"Long Beach notebook: JR Hildebrand breaks hand""Hildebrand cleared to return at Phoenix"the original"Bourdais to undergo surgery on multiple fractures""Aleshin loses Schmidt Peterson IndyCar ride""Saavedra in at SPM for Pocono, Gateway"the original"Bourdais to make return at Gateway"the original"The IndyCar Grand Prix no longer is sponsored by Angie's List""2017 IndyCar Series rulebook""2017 Verizon IndyCar Series Official Rulebook"Official websiteeeeee