Skip to main content

awk assign to multiple variables at once [Resolved]

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);
                  # 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.

Question Credit: villapx
Question Reference
Asked March 23, 2019
Tags: , gawk
Posted Under: Unix Linux
4 Answers

Your Answer