AWK: why does <(cat) work for stdin, but $* doesn't?
echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"
The above syntax works fine with the calculated result '1337'.
echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"
But the above syntax doesn't work, though there's no error.
Plz advise.
awk
add a comment |
echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"
The above syntax works fine with the calculated result '1337'.
echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"
But the above syntax doesn't work, though there's no error.
Plz advise.
awk
What are you expecting it to do? And do you want to ask about$(cat)
or<(cat)
? They are two very different things.
– terdon♦
38 mins ago
add a comment |
echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"
The above syntax works fine with the calculated result '1337'.
echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"
But the above syntax doesn't work, though there's no error.
Plz advise.
awk
echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"
The above syntax works fine with the calculated result '1337'.
echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"
But the above syntax doesn't work, though there's no error.
Plz advise.
awk
awk
edited 39 mins ago
terdon♦
64.3k12136212
64.3k12136212
asked 55 mins ago
user58029
184
184
What are you expecting it to do? And do you want to ask about$(cat)
or<(cat)
? They are two very different things.
– terdon♦
38 mins ago
add a comment |
What are you expecting it to do? And do you want to ask about$(cat)
or<(cat)
? They are two very different things.
– terdon♦
38 mins ago
What are you expecting it to do? And do you want to ask about
$(cat)
or <(cat)
? They are two very different things.– terdon♦
38 mins ago
What are you expecting it to do? And do you want to ask about
$(cat)
or <(cat)
? They are two very different things.– terdon♦
38 mins ago
add a comment |
1 Answer
1
active
oldest
votes
The $(command)
syntax will return the output of command
. Here, you are using the very simple cat
program whose only job is to copy everything from standard input (stdin) into standard output (stdout). Since you are running the awk
script inside double quotes, the $(cat)
is expanded by the shell before the awk
script is run, so it reads the echo
output into its stdin and duly copies it to its stdout. This is then passed to the awk
script. You can see this in action with set -x
:
$ set -x
$ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"
+ echo '((3+(2^3)) * 34^2 / 9)-75.89'
++ cat
+ awk 'BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }'
1337
So, awk
is actually running BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }'
which returns 1337.
Now, the $*
is a special shell variable that expands to all the positional parameters given to a shell script (see man bash
):
* Expands to the positional parameters, starting from one. When the expan‐
sion is not within double quotes, each positional parameter expands to a
separate word. In contexts where it is performed, those words are sub‐
ject to further word splitting and pathname expansion. When the expan‐
sion occurs within double quotes, it expands to a single word with the
value of each parameter separated by the first character of the IFS spe‐
cial variable. That is, "$*" is equivalent to "$1c$2c...", where c is
the first character of the value of the IFS variable. If IFS is unset,
the parameters are separated by spaces. If IFS is null, the parameters
are joined without intervening separators.
However, this variable is empty here since there is no shell script and no parameters. Therefore, the awk
script becomes:
$ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"
+ awk 'BEGIN{ print }'
+ echo '((3+(2^3)) * 34^2 / 9)-75.89'
The $*
expands to an empty string, and awk
is told to print an empty string, and this is why you get no output.
You might want to just use bc
instead:
$ echo '((3+(2^3)) * 34^2 / 9)-75.89' | bc
1336.11
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2faskubuntu.com%2fquestions%2f1106132%2fawk-why-does-cat-work-for-stdin-but-doesnt%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The $(command)
syntax will return the output of command
. Here, you are using the very simple cat
program whose only job is to copy everything from standard input (stdin) into standard output (stdout). Since you are running the awk
script inside double quotes, the $(cat)
is expanded by the shell before the awk
script is run, so it reads the echo
output into its stdin and duly copies it to its stdout. This is then passed to the awk
script. You can see this in action with set -x
:
$ set -x
$ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"
+ echo '((3+(2^3)) * 34^2 / 9)-75.89'
++ cat
+ awk 'BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }'
1337
So, awk
is actually running BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }'
which returns 1337.
Now, the $*
is a special shell variable that expands to all the positional parameters given to a shell script (see man bash
):
* Expands to the positional parameters, starting from one. When the expan‐
sion is not within double quotes, each positional parameter expands to a
separate word. In contexts where it is performed, those words are sub‐
ject to further word splitting and pathname expansion. When the expan‐
sion occurs within double quotes, it expands to a single word with the
value of each parameter separated by the first character of the IFS spe‐
cial variable. That is, "$*" is equivalent to "$1c$2c...", where c is
the first character of the value of the IFS variable. If IFS is unset,
the parameters are separated by spaces. If IFS is null, the parameters
are joined without intervening separators.
However, this variable is empty here since there is no shell script and no parameters. Therefore, the awk
script becomes:
$ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"
+ awk 'BEGIN{ print }'
+ echo '((3+(2^3)) * 34^2 / 9)-75.89'
The $*
expands to an empty string, and awk
is told to print an empty string, and this is why you get no output.
You might want to just use bc
instead:
$ echo '((3+(2^3)) * 34^2 / 9)-75.89' | bc
1336.11
add a comment |
The $(command)
syntax will return the output of command
. Here, you are using the very simple cat
program whose only job is to copy everything from standard input (stdin) into standard output (stdout). Since you are running the awk
script inside double quotes, the $(cat)
is expanded by the shell before the awk
script is run, so it reads the echo
output into its stdin and duly copies it to its stdout. This is then passed to the awk
script. You can see this in action with set -x
:
$ set -x
$ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"
+ echo '((3+(2^3)) * 34^2 / 9)-75.89'
++ cat
+ awk 'BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }'
1337
So, awk
is actually running BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }'
which returns 1337.
Now, the $*
is a special shell variable that expands to all the positional parameters given to a shell script (see man bash
):
* Expands to the positional parameters, starting from one. When the expan‐
sion is not within double quotes, each positional parameter expands to a
separate word. In contexts where it is performed, those words are sub‐
ject to further word splitting and pathname expansion. When the expan‐
sion occurs within double quotes, it expands to a single word with the
value of each parameter separated by the first character of the IFS spe‐
cial variable. That is, "$*" is equivalent to "$1c$2c...", where c is
the first character of the value of the IFS variable. If IFS is unset,
the parameters are separated by spaces. If IFS is null, the parameters
are joined without intervening separators.
However, this variable is empty here since there is no shell script and no parameters. Therefore, the awk
script becomes:
$ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"
+ awk 'BEGIN{ print }'
+ echo '((3+(2^3)) * 34^2 / 9)-75.89'
The $*
expands to an empty string, and awk
is told to print an empty string, and this is why you get no output.
You might want to just use bc
instead:
$ echo '((3+(2^3)) * 34^2 / 9)-75.89' | bc
1336.11
add a comment |
The $(command)
syntax will return the output of command
. Here, you are using the very simple cat
program whose only job is to copy everything from standard input (stdin) into standard output (stdout). Since you are running the awk
script inside double quotes, the $(cat)
is expanded by the shell before the awk
script is run, so it reads the echo
output into its stdin and duly copies it to its stdout. This is then passed to the awk
script. You can see this in action with set -x
:
$ set -x
$ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"
+ echo '((3+(2^3)) * 34^2 / 9)-75.89'
++ cat
+ awk 'BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }'
1337
So, awk
is actually running BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }'
which returns 1337.
Now, the $*
is a special shell variable that expands to all the positional parameters given to a shell script (see man bash
):
* Expands to the positional parameters, starting from one. When the expan‐
sion is not within double quotes, each positional parameter expands to a
separate word. In contexts where it is performed, those words are sub‐
ject to further word splitting and pathname expansion. When the expan‐
sion occurs within double quotes, it expands to a single word with the
value of each parameter separated by the first character of the IFS spe‐
cial variable. That is, "$*" is equivalent to "$1c$2c...", where c is
the first character of the value of the IFS variable. If IFS is unset,
the parameters are separated by spaces. If IFS is null, the parameters
are joined without intervening separators.
However, this variable is empty here since there is no shell script and no parameters. Therefore, the awk
script becomes:
$ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"
+ awk 'BEGIN{ print }'
+ echo '((3+(2^3)) * 34^2 / 9)-75.89'
The $*
expands to an empty string, and awk
is told to print an empty string, and this is why you get no output.
You might want to just use bc
instead:
$ echo '((3+(2^3)) * 34^2 / 9)-75.89' | bc
1336.11
The $(command)
syntax will return the output of command
. Here, you are using the very simple cat
program whose only job is to copy everything from standard input (stdin) into standard output (stdout). Since you are running the awk
script inside double quotes, the $(cat)
is expanded by the shell before the awk
script is run, so it reads the echo
output into its stdin and duly copies it to its stdout. This is then passed to the awk
script. You can see this in action with set -x
:
$ set -x
$ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"
+ echo '((3+(2^3)) * 34^2 / 9)-75.89'
++ cat
+ awk 'BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }'
1337
So, awk
is actually running BEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }'
which returns 1337.
Now, the $*
is a special shell variable that expands to all the positional parameters given to a shell script (see man bash
):
* Expands to the positional parameters, starting from one. When the expan‐
sion is not within double quotes, each positional parameter expands to a
separate word. In contexts where it is performed, those words are sub‐
ject to further word splitting and pathname expansion. When the expan‐
sion occurs within double quotes, it expands to a single word with the
value of each parameter separated by the first character of the IFS spe‐
cial variable. That is, "$*" is equivalent to "$1c$2c...", where c is
the first character of the value of the IFS variable. If IFS is unset,
the parameters are separated by spaces. If IFS is null, the parameters
are joined without intervening separators.
However, this variable is empty here since there is no shell script and no parameters. Therefore, the awk
script becomes:
$ echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"
+ awk 'BEGIN{ print }'
+ echo '((3+(2^3)) * 34^2 / 9)-75.89'
The $*
expands to an empty string, and awk
is told to print an empty string, and this is why you get no output.
You might want to just use bc
instead:
$ echo '((3+(2^3)) * 34^2 / 9)-75.89' | bc
1336.11
edited 26 mins ago
answered 32 mins ago
terdon♦
64.3k12136212
64.3k12136212
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2faskubuntu.com%2fquestions%2f1106132%2fawk-why-does-cat-work-for-stdin-but-doesnt%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
What are you expecting it to do? And do you want to ask about
$(cat)
or<(cat)
? They are two very different things.– terdon♦
38 mins ago