awk assign to multiple variables at once2019 Community Moderator ElectionAWK Search massive file and write to variable nameThe ERE regex to split() string between a delimiter and end-of-wordHow to Save variables in a script that can be shared between two runs of awk against the same input file in the script?Using bash variable with escape character in awk to extract lines from fileAdding Contents of multiple files using awkpassing two variable streams as input to awk within a scriptAwk Command - combine two commandssetting more variables insite awk if ? :processing multiple files with awk problemWhy does gawk treat `0123` as a decimal number when coming from the input data?

Change the color of a single dot in `ddot` symbol

I found an audio circuit and I built it just fine, but I find it a bit too quiet. How do I amplify the output so that it is a bit louder?

Is this part of the description of the Archfey warlock's Misty Escape feature redundant?

How much of a Devil Fruit must be consumed to gain the power?

The IT department bottlenecks progress, how should I handle this?

How do I tell my boss that I'm quitting soon, especially given that a colleague just left this week

What is the difference between lands and mana?

How would you translate "more" for use as an interface button?

What features enable the Su-25 Frogfoot to operate with such a wide variety of fuels?

Delete multiple columns using awk or sed

Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?

Did the UK lift the requirement for registering SIM cards?

Quoting Keynes in a lecture

Giving feedback to someone without sounding prejudiced

When were female captains banned from Starfleet?

How much theory knowledge is actually used while playing?

The Digit Triangles

Multiplicative persistence

Make a Bowl of Alphabet Soup

What does "Scientists rise up against statistical significance" mean? (Comment in Nature)

What to do when eye contact makes your coworker uncomfortable?

Pre-mixing cryogenic fuels and using only one fuel tank

Why does AES have exactly 10 rounds for a 128-bit key, 12 for 192 bits and 14 for a 256-bit key size?

How to make money from a browser who sees 5 seconds into the future of any web page?



awk assign to multiple variables at once



2019 Community Moderator ElectionAWK Search massive file and write to variable nameThe ERE regex to split() string between a delimiter and end-of-wordHow to Save variables in a script that can be shared between two runs of awk against the same input file in the script?Using bash variable with escape character in awk to extract lines from fileAdding Contents of multiple files using awkpassing two variable streams as input to awk within a scriptAwk Command - combine two commandssetting more variables insite awk if ? :processing multiple files with awk problemWhy does gawk treat `0123` as a decimal number when coming from the input data?










5















I'm trying to pull two numerical values out of a string and assign them to variables using awk (gawk is what I'm using specifically). I want to pull the major and minor version numbers out of a tmux version string into awk variables, e.g.:



  • input: tmux 2.8; maj == 2 and min == 8

  • input: tmux 1.9a; maj == 1 and min == 9

  • input: tmux 2.10; maj == 2 and min == 10

Assuming my input comes from tmux -V on stdin, I currently have the following:



tmux -V | awk '
maj = +gensub(/([0-9]+)..*/, "\1", "g", $2);
min = +gensub(/.*.([0-9]+).*/, "\1", "g", $2);
# ...do something with maj and min...
'


This works, but as many users of tmux know, using if-shell in the .tmux.conf file (where I hope to use this stuff) can easily lead to really long lines in the config file, so I'm wondering if there's a way to combine these two variable assignments into one statement to save space...or any other way to glean these two variables from the input and save space.



I'm thinking of something like:



awk ' maj, min = +gensub(/([0-9]+).([0-9]+).*/, "\1 \2", "g", $2); '


...kind of like in Python, but that particular syntax doesn't exist in awk. Is there anything else that's possible?



Note that readability isn't really a concern, just length.










share|improve this question


























    5















    I'm trying to pull two numerical values out of a string and assign them to variables using awk (gawk is what I'm using specifically). I want to pull the major and minor version numbers out of a tmux version string into awk variables, e.g.:



    • input: tmux 2.8; maj == 2 and min == 8

    • input: tmux 1.9a; maj == 1 and min == 9

    • input: tmux 2.10; maj == 2 and min == 10

    Assuming my input comes from tmux -V on stdin, I currently have the following:



    tmux -V | awk '
    maj = +gensub(/([0-9]+)..*/, "\1", "g", $2);
    min = +gensub(/.*.([0-9]+).*/, "\1", "g", $2);
    # ...do something with maj and min...
    '


    This works, but as many users of tmux know, using if-shell in the .tmux.conf file (where I hope to use this stuff) can easily lead to really long lines in the config file, so I'm wondering if there's a way to combine these two variable assignments into one statement to save space...or any other way to glean these two variables from the input and save space.



    I'm thinking of something like:



    awk ' maj, min = +gensub(/([0-9]+).([0-9]+).*/, "\1 \2", "g", $2); '


    ...kind of like in Python, but that particular syntax doesn't exist in awk. Is there anything else that's possible?



    Note that readability isn't really a concern, just length.










    share|improve this question
























      5












      5








      5








      I'm trying to pull two numerical values out of a string and assign them to variables using awk (gawk is what I'm using specifically). I want to pull the major and minor version numbers out of a tmux version string into awk variables, e.g.:



      • input: tmux 2.8; maj == 2 and min == 8

      • input: tmux 1.9a; maj == 1 and min == 9

      • input: tmux 2.10; maj == 2 and min == 10

      Assuming my input comes from tmux -V on stdin, I currently have the following:



      tmux -V | awk '
      maj = +gensub(/([0-9]+)..*/, "\1", "g", $2);
      min = +gensub(/.*.([0-9]+).*/, "\1", "g", $2);
      # ...do something with maj and min...
      '


      This works, but as many users of tmux know, using if-shell in the .tmux.conf file (where I hope to use this stuff) can easily lead to really long lines in the config file, so I'm wondering if there's a way to combine these two variable assignments into one statement to save space...or any other way to glean these two variables from the input and save space.



      I'm thinking of something like:



      awk ' maj, min = +gensub(/([0-9]+).([0-9]+).*/, "\1 \2", "g", $2); '


      ...kind of like in Python, but that particular syntax doesn't exist in awk. Is there anything else that's possible?



      Note that readability isn't really a concern, just length.










      share|improve this question














      I'm trying to pull two numerical values out of a string and assign them to variables using awk (gawk is what I'm using specifically). I want to pull the major and minor version numbers out of a tmux version string into awk variables, e.g.:



      • input: tmux 2.8; maj == 2 and min == 8

      • input: tmux 1.9a; maj == 1 and min == 9

      • input: tmux 2.10; maj == 2 and min == 10

      Assuming my input comes from tmux -V on stdin, I currently have the following:



      tmux -V | awk '
      maj = +gensub(/([0-9]+)..*/, "\1", "g", $2);
      min = +gensub(/.*.([0-9]+).*/, "\1", "g", $2);
      # ...do something with maj and min...
      '


      This works, but as many users of tmux know, using if-shell in the .tmux.conf file (where I hope to use this stuff) can easily lead to really long lines in the config file, so I'm wondering if there's a way to combine these two variable assignments into one statement to save space...or any other way to glean these two variables from the input and save space.



      I'm thinking of something like:



      awk ' maj, min = +gensub(/([0-9]+).([0-9]+).*/, "\1 \2", "g", $2); '


      ...kind of like in Python, but that particular syntax doesn't exist in awk. Is there anything else that's possible?



      Note that readability isn't really a concern, just length.







      awk gawk






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 9 hours ago









      villapxvillapx

      26028




      26028




















          3 Answers
          3






          active

          oldest

          votes


















          6














          Note that gensub is a gawk extension, it won't work with any other awk implementation. Also note that the + unary operator doesn't force numeric conversion in all awk implementations, using + 0 is more portable.



          Here you could do:



          tmux -V | awk -F '[ .]' 'maj = $2+0; min = $3+0; print maj, min'


          If you don't mind using GNU awk extensions, you could also do:



          tmux -V | awk -v FPAT='[0-9]+' 'maj = $1; min = $2; print maj, min'





          share|improve this answer























          • Thanks for the additional explanations on compatibility!

            – villapx
            5 hours ago


















          8














          Since you're using GNU awk, you can use the 3-arg form of match() to store multiple capturing groups:



          awk '
          match($0, /([0-9]+).([0-9]+)/, m) maj=m[1]; min=m[2]; print maj, min
          ' <<END
          tmux 2.8
          tmux 1.9a
          tmux 2.10
          END




          2 8
          1 9
          2 10


          https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html






          share|improve this answer
































            1














            Another user posted this answer, and it later was deleted. I thought it was useful:



            Using the split() function, split the version string into an array ver, then access ver[1] and ver[2] rather than maj and min, respectively (or simply store the values in those variables):



            tmux -V | awk ' split($2, ver, /[.a-z]/); print ver[1], ver[2] '


            The plus here is that split() isn't a gawk extension (though its optional fourth argument seps is).






            share|improve this answer
























              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%2f507768%2fawk-assign-to-multiple-variables-at-once%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









              6














              Note that gensub is a gawk extension, it won't work with any other awk implementation. Also note that the + unary operator doesn't force numeric conversion in all awk implementations, using + 0 is more portable.



              Here you could do:



              tmux -V | awk -F '[ .]' 'maj = $2+0; min = $3+0; print maj, min'


              If you don't mind using GNU awk extensions, you could also do:



              tmux -V | awk -v FPAT='[0-9]+' 'maj = $1; min = $2; print maj, min'





              share|improve this answer























              • Thanks for the additional explanations on compatibility!

                – villapx
                5 hours ago















              6














              Note that gensub is a gawk extension, it won't work with any other awk implementation. Also note that the + unary operator doesn't force numeric conversion in all awk implementations, using + 0 is more portable.



              Here you could do:



              tmux -V | awk -F '[ .]' 'maj = $2+0; min = $3+0; print maj, min'


              If you don't mind using GNU awk extensions, you could also do:



              tmux -V | awk -v FPAT='[0-9]+' 'maj = $1; min = $2; print maj, min'





              share|improve this answer























              • Thanks for the additional explanations on compatibility!

                – villapx
                5 hours ago













              6












              6








              6







              Note that gensub is a gawk extension, it won't work with any other awk implementation. Also note that the + unary operator doesn't force numeric conversion in all awk implementations, using + 0 is more portable.



              Here you could do:



              tmux -V | awk -F '[ .]' 'maj = $2+0; min = $3+0; print maj, min'


              If you don't mind using GNU awk extensions, you could also do:



              tmux -V | awk -v FPAT='[0-9]+' 'maj = $1; min = $2; print maj, min'





              share|improve this answer













              Note that gensub is a gawk extension, it won't work with any other awk implementation. Also note that the + unary operator doesn't force numeric conversion in all awk implementations, using + 0 is more portable.



              Here you could do:



              tmux -V | awk -F '[ .]' 'maj = $2+0; min = $3+0; print maj, min'


              If you don't mind using GNU awk extensions, you could also do:



              tmux -V | awk -v FPAT='[0-9]+' 'maj = $1; min = $2; print maj, min'






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 8 hours ago









              Stéphane ChazelasStéphane Chazelas

              311k57586945




              311k57586945












              • Thanks for the additional explanations on compatibility!

                – villapx
                5 hours ago

















              • Thanks for the additional explanations on compatibility!

                – villapx
                5 hours ago
















              Thanks for the additional explanations on compatibility!

              – villapx
              5 hours ago





              Thanks for the additional explanations on compatibility!

              – villapx
              5 hours ago













              8














              Since you're using GNU awk, you can use the 3-arg form of match() to store multiple capturing groups:



              awk '
              match($0, /([0-9]+).([0-9]+)/, m) maj=m[1]; min=m[2]; print maj, min
              ' <<END
              tmux 2.8
              tmux 1.9a
              tmux 2.10
              END




              2 8
              1 9
              2 10


              https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html






              share|improve this answer





























                8














                Since you're using GNU awk, you can use the 3-arg form of match() to store multiple capturing groups:



                awk '
                match($0, /([0-9]+).([0-9]+)/, m) maj=m[1]; min=m[2]; print maj, min
                ' <<END
                tmux 2.8
                tmux 1.9a
                tmux 2.10
                END




                2 8
                1 9
                2 10


                https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html






                share|improve this answer



























                  8












                  8








                  8







                  Since you're using GNU awk, you can use the 3-arg form of match() to store multiple capturing groups:



                  awk '
                  match($0, /([0-9]+).([0-9]+)/, m) maj=m[1]; min=m[2]; print maj, min
                  ' <<END
                  tmux 2.8
                  tmux 1.9a
                  tmux 2.10
                  END




                  2 8
                  1 9
                  2 10


                  https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html






                  share|improve this answer















                  Since you're using GNU awk, you can use the 3-arg form of match() to store multiple capturing groups:



                  awk '
                  match($0, /([0-9]+).([0-9]+)/, m) maj=m[1]; min=m[2]; print maj, min
                  ' <<END
                  tmux 2.8
                  tmux 1.9a
                  tmux 2.10
                  END




                  2 8
                  1 9
                  2 10


                  https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 8 hours ago

























                  answered 8 hours ago









                  glenn jackmanglenn jackman

                  52.5k573113




                  52.5k573113





















                      1














                      Another user posted this answer, and it later was deleted. I thought it was useful:



                      Using the split() function, split the version string into an array ver, then access ver[1] and ver[2] rather than maj and min, respectively (or simply store the values in those variables):



                      tmux -V | awk ' split($2, ver, /[.a-z]/); print ver[1], ver[2] '


                      The plus here is that split() isn't a gawk extension (though its optional fourth argument seps is).






                      share|improve this answer





























                        1














                        Another user posted this answer, and it later was deleted. I thought it was useful:



                        Using the split() function, split the version string into an array ver, then access ver[1] and ver[2] rather than maj and min, respectively (or simply store the values in those variables):



                        tmux -V | awk ' split($2, ver, /[.a-z]/); print ver[1], ver[2] '


                        The plus here is that split() isn't a gawk extension (though its optional fourth argument seps is).






                        share|improve this answer



























                          1












                          1








                          1







                          Another user posted this answer, and it later was deleted. I thought it was useful:



                          Using the split() function, split the version string into an array ver, then access ver[1] and ver[2] rather than maj and min, respectively (or simply store the values in those variables):



                          tmux -V | awk ' split($2, ver, /[.a-z]/); print ver[1], ver[2] '


                          The plus here is that split() isn't a gawk extension (though its optional fourth argument seps is).






                          share|improve this answer















                          Another user posted this answer, and it later was deleted. I thought it was useful:



                          Using the split() function, split the version string into an array ver, then access ver[1] and ver[2] rather than maj and min, respectively (or simply store the values in those variables):



                          tmux -V | awk ' split($2, ver, /[.a-z]/); print ver[1], ver[2] '


                          The plus here is that split() isn't a gawk extension (though its optional fourth argument seps is).







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 3 hours ago









                          Stéphane Chazelas

                          311k57586945




                          311k57586945










                          answered 5 hours ago









                          villapxvillapx

                          26028




                          26028



























                              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%2f507768%2fawk-assign-to-multiple-variables-at-once%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Reverse int within the 32-bit signed integer range: [−2^31, 2^31 − 1]Combining two 32-bit integers into one 64-bit integerDetermine if an int is within rangeLossy packing 32 bit integer to 16 bitComputing the square root of a 64-bit integerKeeping integer addition within boundsSafe multiplication of two 64-bit signed integersLeetcode 10: Regular Expression MatchingSigned integer-to-ascii x86_64 assembler macroReverse the digits of an Integer“Add two numbers given in reverse order from a linked list”

                              Category:Fedor von Bock Media in category "Fedor von Bock"Navigation menuUpload mediaISNI: 0000 0000 5511 3417VIAF ID: 24712551GND ID: 119294796Library of Congress authority ID: n96068363BnF ID: 12534305fSUDOC authorities ID: 034604189Open Library ID: OL338253ANKCR AUT ID: jn19990000869National Library of Israel ID: 000514068National Thesaurus for Author Names ID: 341574317ReasonatorScholiaStatistics

                              Kiel Indholdsfortegnelse Historie | Transport og færgeforbindelser | Sejlsport og anden sport | Kultur | Kendte personer fra Kiel | Noter | Litteratur | Eksterne henvisninger | Navigationsmenuwww.kiel.de54°19′31″N 10°8′26″Ø / 54.32528°N 10.14056°Ø / 54.32528; 10.14056Oberbürgermeister Dr. Ulf Kämpferwww.statistik-nord.deDen danske Stats StatistikKiels hjemmesiderrrWorldCat312794080n790547494030481-4