How can I feed a string to awk for the purpose of mathematical calculation?
#!/bin/sh --
expression_to_evaluate='12 + 3'
printf '%sn' "Arithemtic Expansion: $(( $expression_to_evaluate ))"
printf '%s' 'bc: '
printf '%sn' "$expression_to_evaluate" | bc
printf '%s' 'awk: '
awk -v expression_to_evaluate="$expression_to_evaluate" -- 'BEGIN{printf "%dn", expression_to_evaluate}'
Output:
Arithemtic Expansion: 15
bc: 15
awk: 12
awk
is returning 12 instead of 15. How can I feed a string to awk
for the purpose of mathematical calculation?
awk posix math
add a comment |
#!/bin/sh --
expression_to_evaluate='12 + 3'
printf '%sn' "Arithemtic Expansion: $(( $expression_to_evaluate ))"
printf '%s' 'bc: '
printf '%sn' "$expression_to_evaluate" | bc
printf '%s' 'awk: '
awk -v expression_to_evaluate="$expression_to_evaluate" -- 'BEGIN{printf "%dn", expression_to_evaluate}'
Output:
Arithemtic Expansion: 15
bc: 15
awk: 12
awk
is returning 12 instead of 15. How can I feed a string to awk
for the purpose of mathematical calculation?
awk posix math
1
Should it also be able to handle expressions like1 + system("date +%s")
or++var
or++a[1]
orENVIRON["SHLVL"] + 3
. In other words, how do you define mathematical calculation?
– Stéphane Chazelas
8 hours ago
add a comment |
#!/bin/sh --
expression_to_evaluate='12 + 3'
printf '%sn' "Arithemtic Expansion: $(( $expression_to_evaluate ))"
printf '%s' 'bc: '
printf '%sn' "$expression_to_evaluate" | bc
printf '%s' 'awk: '
awk -v expression_to_evaluate="$expression_to_evaluate" -- 'BEGIN{printf "%dn", expression_to_evaluate}'
Output:
Arithemtic Expansion: 15
bc: 15
awk: 12
awk
is returning 12 instead of 15. How can I feed a string to awk
for the purpose of mathematical calculation?
awk posix math
#!/bin/sh --
expression_to_evaluate='12 + 3'
printf '%sn' "Arithemtic Expansion: $(( $expression_to_evaluate ))"
printf '%s' 'bc: '
printf '%sn' "$expression_to_evaluate" | bc
printf '%s' 'awk: '
awk -v expression_to_evaluate="$expression_to_evaluate" -- 'BEGIN{printf "%dn", expression_to_evaluate}'
Output:
Arithemtic Expansion: 15
bc: 15
awk: 12
awk
is returning 12 instead of 15. How can I feed a string to awk
for the purpose of mathematical calculation?
awk posix math
awk posix math
edited 2 hours ago
Harold Fischer
asked 20 hours ago
Harold FischerHarold Fischer
668415
668415
1
Should it also be able to handle expressions like1 + system("date +%s")
or++var
or++a[1]
orENVIRON["SHLVL"] + 3
. In other words, how do you define mathematical calculation?
– Stéphane Chazelas
8 hours ago
add a comment |
1
Should it also be able to handle expressions like1 + system("date +%s")
or++var
or++a[1]
orENVIRON["SHLVL"] + 3
. In other words, how do you define mathematical calculation?
– Stéphane Chazelas
8 hours ago
1
1
Should it also be able to handle expressions like
1 + system("date +%s")
or ++var
or ++a[1]
or ENVIRON["SHLVL"] + 3
. In other words, how do you define mathematical calculation?– Stéphane Chazelas
8 hours ago
Should it also be able to handle expressions like
1 + system("date +%s")
or ++var
or ++a[1]
or ENVIRON["SHLVL"] + 3
. In other words, how do you define mathematical calculation?– Stéphane Chazelas
8 hours ago
add a comment |
4 Answers
4
active
oldest
votes
While this works, I recommend against it because, unless you control the source of expression_to_evaluate
, it is a security risk:
$ expression_to_evaluate='12 + 3'
$ awk "BEGIN{printf "%dn", $expression_to_evaluate}"
15
1
Note thatecho "$(($expression_to_evaluate))"
is also an arbitrary command injection vulnerability in bash/ksh/zsh, and even indash
oryash
can be abused to set sensitive variables like PATH or IFS. But the OP is more or less asking for arbitrary code to be evaluated here.
– Stéphane Chazelas
7 hours ago
add a comment |
awk
does not have a way to evaluate a general arithmetic expression given to it as a string. bc
, on the other hand, takes an expression and evaluates it, as does the shell.
Would you want to evaluate the string as a mathematical expression in awk
, you would either have to inject the expression as code into the awk
program (potentially creating a code injection vulnerability), or simply write your own parser for the types of expressions you'd like to evaluate (this is a common computer science homework assignment).
You get 12
from your awk
code because the value of expression_to_evaluate
, when converted into an integer, can be converted up until the space character in the string. This is how the strtol()
C library function works when it converts a string into an integer, and awk
is likely using this under the hood.
"You would either have to inject the expression as code". How is that different frombc
?bc
can't even take external input, all you can do is inject code to it.
– Stéphane Chazelas
8 hours ago
@StéphaneChazelas The difference is that it's the normal mode of operation forbc
to read expressions from standard input. It is not the normal mode of operation ofawk
to insert shell code (user input) into the string that serves as the actual code.
– Kusalananda
6 hours ago
add a comment |
echo $expression_to_evaluate | awk -F "+" '{print ($1+$2)}'
They didn't ask for static addition, they askedHow can I feed a string to awk for the purpose of mathematical calculation?
– Jeff Schaller
6 hours ago
add a comment |
if You want to add 2 numbers using awk use below command
awk '{print 12 + 3;exit}'
output
15
They didn't ask for static addition, they askedHow can I feed a string to awk for the purpose of mathematical calculation?
– Jeff Schaller
6 hours ago
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%2f503691%2fhow-can-i-feed-a-string-to-awk-for-the-purpose-of-mathematical-calculation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
While this works, I recommend against it because, unless you control the source of expression_to_evaluate
, it is a security risk:
$ expression_to_evaluate='12 + 3'
$ awk "BEGIN{printf "%dn", $expression_to_evaluate}"
15
1
Note thatecho "$(($expression_to_evaluate))"
is also an arbitrary command injection vulnerability in bash/ksh/zsh, and even indash
oryash
can be abused to set sensitive variables like PATH or IFS. But the OP is more or less asking for arbitrary code to be evaluated here.
– Stéphane Chazelas
7 hours ago
add a comment |
While this works, I recommend against it because, unless you control the source of expression_to_evaluate
, it is a security risk:
$ expression_to_evaluate='12 + 3'
$ awk "BEGIN{printf "%dn", $expression_to_evaluate}"
15
1
Note thatecho "$(($expression_to_evaluate))"
is also an arbitrary command injection vulnerability in bash/ksh/zsh, and even indash
oryash
can be abused to set sensitive variables like PATH or IFS. But the OP is more or less asking for arbitrary code to be evaluated here.
– Stéphane Chazelas
7 hours ago
add a comment |
While this works, I recommend against it because, unless you control the source of expression_to_evaluate
, it is a security risk:
$ expression_to_evaluate='12 + 3'
$ awk "BEGIN{printf "%dn", $expression_to_evaluate}"
15
While this works, I recommend against it because, unless you control the source of expression_to_evaluate
, it is a security risk:
$ expression_to_evaluate='12 + 3'
$ awk "BEGIN{printf "%dn", $expression_to_evaluate}"
15
answered 20 hours ago
John1024John1024
47.3k5110125
47.3k5110125
1
Note thatecho "$(($expression_to_evaluate))"
is also an arbitrary command injection vulnerability in bash/ksh/zsh, and even indash
oryash
can be abused to set sensitive variables like PATH or IFS. But the OP is more or less asking for arbitrary code to be evaluated here.
– Stéphane Chazelas
7 hours ago
add a comment |
1
Note thatecho "$(($expression_to_evaluate))"
is also an arbitrary command injection vulnerability in bash/ksh/zsh, and even indash
oryash
can be abused to set sensitive variables like PATH or IFS. But the OP is more or less asking for arbitrary code to be evaluated here.
– Stéphane Chazelas
7 hours ago
1
1
Note that
echo "$(($expression_to_evaluate))"
is also an arbitrary command injection vulnerability in bash/ksh/zsh, and even in dash
or yash
can be abused to set sensitive variables like PATH or IFS. But the OP is more or less asking for arbitrary code to be evaluated here.– Stéphane Chazelas
7 hours ago
Note that
echo "$(($expression_to_evaluate))"
is also an arbitrary command injection vulnerability in bash/ksh/zsh, and even in dash
or yash
can be abused to set sensitive variables like PATH or IFS. But the OP is more or less asking for arbitrary code to be evaluated here.– Stéphane Chazelas
7 hours ago
add a comment |
awk
does not have a way to evaluate a general arithmetic expression given to it as a string. bc
, on the other hand, takes an expression and evaluates it, as does the shell.
Would you want to evaluate the string as a mathematical expression in awk
, you would either have to inject the expression as code into the awk
program (potentially creating a code injection vulnerability), or simply write your own parser for the types of expressions you'd like to evaluate (this is a common computer science homework assignment).
You get 12
from your awk
code because the value of expression_to_evaluate
, when converted into an integer, can be converted up until the space character in the string. This is how the strtol()
C library function works when it converts a string into an integer, and awk
is likely using this under the hood.
"You would either have to inject the expression as code". How is that different frombc
?bc
can't even take external input, all you can do is inject code to it.
– Stéphane Chazelas
8 hours ago
@StéphaneChazelas The difference is that it's the normal mode of operation forbc
to read expressions from standard input. It is not the normal mode of operation ofawk
to insert shell code (user input) into the string that serves as the actual code.
– Kusalananda
6 hours ago
add a comment |
awk
does not have a way to evaluate a general arithmetic expression given to it as a string. bc
, on the other hand, takes an expression and evaluates it, as does the shell.
Would you want to evaluate the string as a mathematical expression in awk
, you would either have to inject the expression as code into the awk
program (potentially creating a code injection vulnerability), or simply write your own parser for the types of expressions you'd like to evaluate (this is a common computer science homework assignment).
You get 12
from your awk
code because the value of expression_to_evaluate
, when converted into an integer, can be converted up until the space character in the string. This is how the strtol()
C library function works when it converts a string into an integer, and awk
is likely using this under the hood.
"You would either have to inject the expression as code". How is that different frombc
?bc
can't even take external input, all you can do is inject code to it.
– Stéphane Chazelas
8 hours ago
@StéphaneChazelas The difference is that it's the normal mode of operation forbc
to read expressions from standard input. It is not the normal mode of operation ofawk
to insert shell code (user input) into the string that serves as the actual code.
– Kusalananda
6 hours ago
add a comment |
awk
does not have a way to evaluate a general arithmetic expression given to it as a string. bc
, on the other hand, takes an expression and evaluates it, as does the shell.
Would you want to evaluate the string as a mathematical expression in awk
, you would either have to inject the expression as code into the awk
program (potentially creating a code injection vulnerability), or simply write your own parser for the types of expressions you'd like to evaluate (this is a common computer science homework assignment).
You get 12
from your awk
code because the value of expression_to_evaluate
, when converted into an integer, can be converted up until the space character in the string. This is how the strtol()
C library function works when it converts a string into an integer, and awk
is likely using this under the hood.
awk
does not have a way to evaluate a general arithmetic expression given to it as a string. bc
, on the other hand, takes an expression and evaluates it, as does the shell.
Would you want to evaluate the string as a mathematical expression in awk
, you would either have to inject the expression as code into the awk
program (potentially creating a code injection vulnerability), or simply write your own parser for the types of expressions you'd like to evaluate (this is a common computer science homework assignment).
You get 12
from your awk
code because the value of expression_to_evaluate
, when converted into an integer, can be converted up until the space character in the string. This is how the strtol()
C library function works when it converts a string into an integer, and awk
is likely using this under the hood.
answered 19 hours ago
KusalanandaKusalananda
133k17254417
133k17254417
"You would either have to inject the expression as code". How is that different frombc
?bc
can't even take external input, all you can do is inject code to it.
– Stéphane Chazelas
8 hours ago
@StéphaneChazelas The difference is that it's the normal mode of operation forbc
to read expressions from standard input. It is not the normal mode of operation ofawk
to insert shell code (user input) into the string that serves as the actual code.
– Kusalananda
6 hours ago
add a comment |
"You would either have to inject the expression as code". How is that different frombc
?bc
can't even take external input, all you can do is inject code to it.
– Stéphane Chazelas
8 hours ago
@StéphaneChazelas The difference is that it's the normal mode of operation forbc
to read expressions from standard input. It is not the normal mode of operation ofawk
to insert shell code (user input) into the string that serves as the actual code.
– Kusalananda
6 hours ago
"You would either have to inject the expression as code". How is that different from
bc
? bc
can't even take external input, all you can do is inject code to it.– Stéphane Chazelas
8 hours ago
"You would either have to inject the expression as code". How is that different from
bc
? bc
can't even take external input, all you can do is inject code to it.– Stéphane Chazelas
8 hours ago
@StéphaneChazelas The difference is that it's the normal mode of operation for
bc
to read expressions from standard input. It is not the normal mode of operation of awk
to insert shell code (user input) into the string that serves as the actual code.– Kusalananda
6 hours ago
@StéphaneChazelas The difference is that it's the normal mode of operation for
bc
to read expressions from standard input. It is not the normal mode of operation of awk
to insert shell code (user input) into the string that serves as the actual code.– Kusalananda
6 hours ago
add a comment |
echo $expression_to_evaluate | awk -F "+" '{print ($1+$2)}'
They didn't ask for static addition, they askedHow can I feed a string to awk for the purpose of mathematical calculation?
– Jeff Schaller
6 hours ago
add a comment |
echo $expression_to_evaluate | awk -F "+" '{print ($1+$2)}'
They didn't ask for static addition, they askedHow can I feed a string to awk for the purpose of mathematical calculation?
– Jeff Schaller
6 hours ago
add a comment |
echo $expression_to_evaluate | awk -F "+" '{print ($1+$2)}'
echo $expression_to_evaluate | awk -F "+" '{print ($1+$2)}'
answered 19 hours ago
editiniteditinit
1236
1236
They didn't ask for static addition, they askedHow can I feed a string to awk for the purpose of mathematical calculation?
– Jeff Schaller
6 hours ago
add a comment |
They didn't ask for static addition, they askedHow can I feed a string to awk for the purpose of mathematical calculation?
– Jeff Schaller
6 hours ago
They didn't ask for static addition, they asked
How can I feed a string to awk for the purpose of mathematical calculation?
– Jeff Schaller
6 hours ago
They didn't ask for static addition, they asked
How can I feed a string to awk for the purpose of mathematical calculation?
– Jeff Schaller
6 hours ago
add a comment |
if You want to add 2 numbers using awk use below command
awk '{print 12 + 3;exit}'
output
15
They didn't ask for static addition, they askedHow can I feed a string to awk for the purpose of mathematical calculation?
– Jeff Schaller
6 hours ago
add a comment |
if You want to add 2 numbers using awk use below command
awk '{print 12 + 3;exit}'
output
15
They didn't ask for static addition, they askedHow can I feed a string to awk for the purpose of mathematical calculation?
– Jeff Schaller
6 hours ago
add a comment |
if You want to add 2 numbers using awk use below command
awk '{print 12 + 3;exit}'
output
15
if You want to add 2 numbers using awk use below command
awk '{print 12 + 3;exit}'
output
15
answered 8 hours ago
Praveen Kumar BSPraveen Kumar BS
1,5041310
1,5041310
They didn't ask for static addition, they askedHow can I feed a string to awk for the purpose of mathematical calculation?
– Jeff Schaller
6 hours ago
add a comment |
They didn't ask for static addition, they askedHow can I feed a string to awk for the purpose of mathematical calculation?
– Jeff Schaller
6 hours ago
They didn't ask for static addition, they asked
How can I feed a string to awk for the purpose of mathematical calculation?
– Jeff Schaller
6 hours ago
They didn't ask for static addition, they asked
How can I feed a string to awk for the purpose of mathematical calculation?
– Jeff Schaller
6 hours ago
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%2f503691%2fhow-can-i-feed-a-string-to-awk-for-the-purpose-of-mathematical-calculation%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
1
Should it also be able to handle expressions like
1 + system("date +%s")
or++var
or++a[1]
orENVIRON["SHLVL"] + 3
. In other words, how do you define mathematical calculation?– Stéphane Chazelas
8 hours ago