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

                    Mobil Contents History Mobil brands Former Mobil brands Lukoil transaction Mobil UK Mobil Australia Mobil New Zealand Mobil Greece Mobil in Japan Mobil in Canada Mobil Egypt See also References External links Navigation menuwww.mobil.com"Mobil Corporation"the original"Our Houston campus""Business & Finance: Socony-Vacuum Corp.""Popular Mechanics""Lubrite Technologies""Exxon Mobil campus 'clearly happening'""Toledo Blade - Google News Archive Search""The Lion and the Moose - How 2 Executives Pulled off the Biggest Merger Ever""ExxonMobil Press Release""Lubricants""Archived copy"the original"Mobil 1™ and Mobil Super™ motor oil and synthetic motor oil - Mobil™ Motor Oils""Mobil Delvac""Mobil Industrial website""The State of Competition in Gasoline Marketing: The Effects of Refiner Operations at Retail""Mobil Travel Guide to become Forbes Travel Guide""Hotel Rankings: Forbes Merges with Mobil"the original"Jamieson oil industry history""Mobil news""Caltex pumps for control""Watchdog blocks Caltex bid""Exxon Mobil sells service station network""Mobil Oil New Zealand Limited is New Zealand's oldest oil company, with predecessor companies having first established a presence in the country in 1896""ExxonMobil subsidiaries have a business history in New Zealand stretching back more than 120 years. We are involved in petroleum refining and distribution and the marketing of fuels, lubricants and chemical products""Archived copy"the original"Exxon Mobil to Sell Its Japanese Arm for $3.9 Billion""Gas station merger will end Esso and Mobil's long run in Japan""Esso moves to affiliate itself with PC Optimum, no longer Aeroplan, in loyalty point switch""Mobil brand of gas stations to launch in Canada after deal for 213 Loblaws-owned locations""Mobil Nears Completion of Rebranding 200 Loblaw Gas Stations""Learn about ExxonMobil's operations in Egypt""Petrol and Diesel Service Stations in Egypt - Mobil"Official websiteExxon Mobil corporate websiteMobil Industrial official websiteeeeeeeeDA04275022275790-40000 0001 0860 5061n82045453134887257134887257

                    My Life (Mary J. Blige album) Contents Background Critical reception Accolades Commercial performance Track listing Personnel Charts Certifications See also References External links Navigation menu"1. Mary J Blige, My Life - The 50 Best R&B albums of the '90s""American album certifications – Mary J. Blige – My Life""Mary J. Blige's My Life LP (1994) revisited with co-producer Chucky Thompson | Return To The Classics"the original"Key Tracks: Mary J. Blige's My Life""My Life – Mary J. Blige""Worth The Wait""My Life""Forget '411,' Mary J., Better Call 911""Spins"My Life AccoladesThe 500 Greatest Albums of All TimeTime's All-TIME 100 Albums"Top RPM Albums: Issue chartid""Dutchcharts.nl – Mary J. Blige – My Life""Mary J. Blige | Artist | Official Charts""Mary J. Blige Chart History (Billboard 200)""Mary J. Blige Chart History (Top R&B/Hip-Hop Albums)""Canadian album certifications – Mary J Blige – My Life""British album certifications – Mary J Blige – My Life""American album certifications – Mary J Blige – My Life"My LifeMy Life accoladesee

                    Frič See also Navigation menuinternal link