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?
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
andmin == 8
- input:
tmux 1.9a
;maj == 1
andmin == 9
- input:
tmux 2.10
;maj == 2
andmin == 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
add a comment |
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
andmin == 8
- input:
tmux 1.9a
;maj == 1
andmin == 9
- input:
tmux 2.10
;maj == 2
andmin == 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
add a comment |
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
andmin == 8
- input:
tmux 1.9a
;maj == 1
andmin == 9
- input:
tmux 2.10
;maj == 2
andmin == 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
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
andmin == 8
- input:
tmux 1.9a
;maj == 1
andmin == 9
- input:
tmux 2.10
;maj == 2
andmin == 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
awk gawk
asked 9 hours ago
villapxvillapx
26028
26028
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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'
Thanks for the additional explanations on compatibility!
– villapx
5 hours ago
add a comment |
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
add a comment |
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).
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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'
Thanks for the additional explanations on compatibility!
– villapx
5 hours ago
add a comment |
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'
Thanks for the additional explanations on compatibility!
– villapx
5 hours ago
add a comment |
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'
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'
answered 8 hours ago
Stéphane ChazelasStéphane Chazelas
311k57586945
311k57586945
Thanks for the additional explanations on compatibility!
– villapx
5 hours ago
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited 8 hours ago
answered 8 hours ago
glenn jackmanglenn jackman
52.5k573113
52.5k573113
add a comment |
add a comment |
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).
add a comment |
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).
add a comment |
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).
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).
edited 3 hours ago
Stéphane Chazelas
311k57586945
311k57586945
answered 5 hours ago
villapxvillapx
26028
26028
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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