How to prevent printing to stdout from an awk **script** (not from cli)
[GNU awk v4.2.1 on Archlinux]
Suppressing awk
's default print action to stdout from cli is easy and well documented on UL, e.g. here. Doing so from a script gives me headaches. Here's the toy script:
#!/usr/bin/awk -f
BEGIN {FS=","}
FNR > 1 # skip header
{
if ( $1 == $2 ) {
if ( NR == 4 ) {
printf("*** Print NR=4 ok. n")
} else {
print > "/dev/null" # print nothing
}
} else {
printf("=== Fields 1 and 2 not equal (NR=%s). n",NR)
}
}
and toy data:
col1 col2
1,3
2,2
aa,aa
3.01,-353.01
4.1,4.1
101,101
hello, hello
asd,koi0
along with expected / desired output:
=== Fields 1 and 2 not equal (NR=2).
*** Print NR=4 ok.
=== Fields 1 and 2 not equal (NR=5).
=== Fields 1 and 2 not equal (NR=8).
=== Fields 1 and 2 not equal (NR=9).
Instead I get:
=== Fields 1 and 2 not equal (NR=1).
1,3
=== Fields 1 and 2 not equal (NR=2).
2,2
aa,aa
*** Print NR=4 ok.
3.01,-353.01
=== Fields 1 and 2 not equal (NR=5).
4.1,4.1
101,101
hello, hello
=== Fields 1 and 2 not equal (NR=8).
asd,koi0
=== Fields 1 and 2 not equal (NR=9).
To suppress output to stdout, I tried using:
getline
, {}
, next
, printf("")
and even the outlandish ORS=""; print ""; ORS="n"
instead of print > "/dev/null"
. I am obviously doing something very wrong in that frigging script and can not find what ...
awk scripting stdout
add a comment |
[GNU awk v4.2.1 on Archlinux]
Suppressing awk
's default print action to stdout from cli is easy and well documented on UL, e.g. here. Doing so from a script gives me headaches. Here's the toy script:
#!/usr/bin/awk -f
BEGIN {FS=","}
FNR > 1 # skip header
{
if ( $1 == $2 ) {
if ( NR == 4 ) {
printf("*** Print NR=4 ok. n")
} else {
print > "/dev/null" # print nothing
}
} else {
printf("=== Fields 1 and 2 not equal (NR=%s). n",NR)
}
}
and toy data:
col1 col2
1,3
2,2
aa,aa
3.01,-353.01
4.1,4.1
101,101
hello, hello
asd,koi0
along with expected / desired output:
=== Fields 1 and 2 not equal (NR=2).
*** Print NR=4 ok.
=== Fields 1 and 2 not equal (NR=5).
=== Fields 1 and 2 not equal (NR=8).
=== Fields 1 and 2 not equal (NR=9).
Instead I get:
=== Fields 1 and 2 not equal (NR=1).
1,3
=== Fields 1 and 2 not equal (NR=2).
2,2
aa,aa
*** Print NR=4 ok.
3.01,-353.01
=== Fields 1 and 2 not equal (NR=5).
4.1,4.1
101,101
hello, hello
=== Fields 1 and 2 not equal (NR=8).
asd,koi0
=== Fields 1 and 2 not equal (NR=9).
To suppress output to stdout, I tried using:
getline
, {}
, next
, printf("")
and even the outlandish ORS=""; print ""; ORS="n"
instead of print > "/dev/null"
. I am obviously doing something very wrong in that frigging script and can not find what ...
awk scripting stdout
add a comment |
[GNU awk v4.2.1 on Archlinux]
Suppressing awk
's default print action to stdout from cli is easy and well documented on UL, e.g. here. Doing so from a script gives me headaches. Here's the toy script:
#!/usr/bin/awk -f
BEGIN {FS=","}
FNR > 1 # skip header
{
if ( $1 == $2 ) {
if ( NR == 4 ) {
printf("*** Print NR=4 ok. n")
} else {
print > "/dev/null" # print nothing
}
} else {
printf("=== Fields 1 and 2 not equal (NR=%s). n",NR)
}
}
and toy data:
col1 col2
1,3
2,2
aa,aa
3.01,-353.01
4.1,4.1
101,101
hello, hello
asd,koi0
along with expected / desired output:
=== Fields 1 and 2 not equal (NR=2).
*** Print NR=4 ok.
=== Fields 1 and 2 not equal (NR=5).
=== Fields 1 and 2 not equal (NR=8).
=== Fields 1 and 2 not equal (NR=9).
Instead I get:
=== Fields 1 and 2 not equal (NR=1).
1,3
=== Fields 1 and 2 not equal (NR=2).
2,2
aa,aa
*** Print NR=4 ok.
3.01,-353.01
=== Fields 1 and 2 not equal (NR=5).
4.1,4.1
101,101
hello, hello
=== Fields 1 and 2 not equal (NR=8).
asd,koi0
=== Fields 1 and 2 not equal (NR=9).
To suppress output to stdout, I tried using:
getline
, {}
, next
, printf("")
and even the outlandish ORS=""; print ""; ORS="n"
instead of print > "/dev/null"
. I am obviously doing something very wrong in that frigging script and can not find what ...
awk scripting stdout
[GNU awk v4.2.1 on Archlinux]
Suppressing awk
's default print action to stdout from cli is easy and well documented on UL, e.g. here. Doing so from a script gives me headaches. Here's the toy script:
#!/usr/bin/awk -f
BEGIN {FS=","}
FNR > 1 # skip header
{
if ( $1 == $2 ) {
if ( NR == 4 ) {
printf("*** Print NR=4 ok. n")
} else {
print > "/dev/null" # print nothing
}
} else {
printf("=== Fields 1 and 2 not equal (NR=%s). n",NR)
}
}
and toy data:
col1 col2
1,3
2,2
aa,aa
3.01,-353.01
4.1,4.1
101,101
hello, hello
asd,koi0
along with expected / desired output:
=== Fields 1 and 2 not equal (NR=2).
*** Print NR=4 ok.
=== Fields 1 and 2 not equal (NR=5).
=== Fields 1 and 2 not equal (NR=8).
=== Fields 1 and 2 not equal (NR=9).
Instead I get:
=== Fields 1 and 2 not equal (NR=1).
1,3
=== Fields 1 and 2 not equal (NR=2).
2,2
aa,aa
*** Print NR=4 ok.
3.01,-353.01
=== Fields 1 and 2 not equal (NR=5).
4.1,4.1
101,101
hello, hello
=== Fields 1 and 2 not equal (NR=8).
asd,koi0
=== Fields 1 and 2 not equal (NR=9).
To suppress output to stdout, I tried using:
getline
, {}
, next
, printf("")
and even the outlandish ORS=""; print ""; ORS="n"
instead of print > "/dev/null"
. I am obviously doing something very wrong in that frigging script and can not find what ...
awk scripting stdout
awk scripting stdout
asked 3 hours ago
CbhiheCbhihe
3691317
3691317
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The only error is
FNR > 1 # skip header
{
which should be
FNR > 1 { # skip header
A code block with a condition must start on the same line as the condition.
What your original script actually does is first
FNR > 1
This prints all lines from line two onwards (the default action when a condition does not have an associated code block is to print the current record if the condition is true, as if the block had been { print }
).
Then it applies the block following that to each line (since that block does not have an associated condition).
This is not a peculiarity of GNU awk
. All awk
implementations should act like this.
As for the other bits of the script:
} else {
print > "/dev/null" # print nothing
}
This could be deleted, leaving
#!/usr/bin/awk -f
BEGIN { FS = "," }
FNR > 1 {
if ( $1 == $2 ) {
if ( NR == 4 )
printf("*** Print NR=4 ok.n")
} else
printf("=== Fields 1 and 2 not equal (NR=%s).n", NR)
}
or,
#!/usr/bin/awk -f
BEGIN { FS = "," }
FNR == 1 { next }
$1 == $2 && NR == 4 { printf("*** Print NR=4 ok.n") }
$1 != $2 { printf("=== Fields 1 and 2 not equal (NR=%s).n", NR) }
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%2f506180%2fhow-to-prevent-printing-to-stdout-from-an-awk-script-not-from-cli%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 only error is
FNR > 1 # skip header
{
which should be
FNR > 1 { # skip header
A code block with a condition must start on the same line as the condition.
What your original script actually does is first
FNR > 1
This prints all lines from line two onwards (the default action when a condition does not have an associated code block is to print the current record if the condition is true, as if the block had been { print }
).
Then it applies the block following that to each line (since that block does not have an associated condition).
This is not a peculiarity of GNU awk
. All awk
implementations should act like this.
As for the other bits of the script:
} else {
print > "/dev/null" # print nothing
}
This could be deleted, leaving
#!/usr/bin/awk -f
BEGIN { FS = "," }
FNR > 1 {
if ( $1 == $2 ) {
if ( NR == 4 )
printf("*** Print NR=4 ok.n")
} else
printf("=== Fields 1 and 2 not equal (NR=%s).n", NR)
}
or,
#!/usr/bin/awk -f
BEGIN { FS = "," }
FNR == 1 { next }
$1 == $2 && NR == 4 { printf("*** Print NR=4 ok.n") }
$1 != $2 { printf("=== Fields 1 and 2 not equal (NR=%s).n", NR) }
add a comment |
The only error is
FNR > 1 # skip header
{
which should be
FNR > 1 { # skip header
A code block with a condition must start on the same line as the condition.
What your original script actually does is first
FNR > 1
This prints all lines from line two onwards (the default action when a condition does not have an associated code block is to print the current record if the condition is true, as if the block had been { print }
).
Then it applies the block following that to each line (since that block does not have an associated condition).
This is not a peculiarity of GNU awk
. All awk
implementations should act like this.
As for the other bits of the script:
} else {
print > "/dev/null" # print nothing
}
This could be deleted, leaving
#!/usr/bin/awk -f
BEGIN { FS = "," }
FNR > 1 {
if ( $1 == $2 ) {
if ( NR == 4 )
printf("*** Print NR=4 ok.n")
} else
printf("=== Fields 1 and 2 not equal (NR=%s).n", NR)
}
or,
#!/usr/bin/awk -f
BEGIN { FS = "," }
FNR == 1 { next }
$1 == $2 && NR == 4 { printf("*** Print NR=4 ok.n") }
$1 != $2 { printf("=== Fields 1 and 2 not equal (NR=%s).n", NR) }
add a comment |
The only error is
FNR > 1 # skip header
{
which should be
FNR > 1 { # skip header
A code block with a condition must start on the same line as the condition.
What your original script actually does is first
FNR > 1
This prints all lines from line two onwards (the default action when a condition does not have an associated code block is to print the current record if the condition is true, as if the block had been { print }
).
Then it applies the block following that to each line (since that block does not have an associated condition).
This is not a peculiarity of GNU awk
. All awk
implementations should act like this.
As for the other bits of the script:
} else {
print > "/dev/null" # print nothing
}
This could be deleted, leaving
#!/usr/bin/awk -f
BEGIN { FS = "," }
FNR > 1 {
if ( $1 == $2 ) {
if ( NR == 4 )
printf("*** Print NR=4 ok.n")
} else
printf("=== Fields 1 and 2 not equal (NR=%s).n", NR)
}
or,
#!/usr/bin/awk -f
BEGIN { FS = "," }
FNR == 1 { next }
$1 == $2 && NR == 4 { printf("*** Print NR=4 ok.n") }
$1 != $2 { printf("=== Fields 1 and 2 not equal (NR=%s).n", NR) }
The only error is
FNR > 1 # skip header
{
which should be
FNR > 1 { # skip header
A code block with a condition must start on the same line as the condition.
What your original script actually does is first
FNR > 1
This prints all lines from line two onwards (the default action when a condition does not have an associated code block is to print the current record if the condition is true, as if the block had been { print }
).
Then it applies the block following that to each line (since that block does not have an associated condition).
This is not a peculiarity of GNU awk
. All awk
implementations should act like this.
As for the other bits of the script:
} else {
print > "/dev/null" # print nothing
}
This could be deleted, leaving
#!/usr/bin/awk -f
BEGIN { FS = "," }
FNR > 1 {
if ( $1 == $2 ) {
if ( NR == 4 )
printf("*** Print NR=4 ok.n")
} else
printf("=== Fields 1 and 2 not equal (NR=%s).n", NR)
}
or,
#!/usr/bin/awk -f
BEGIN { FS = "," }
FNR == 1 { next }
$1 == $2 && NR == 4 { printf("*** Print NR=4 ok.n") }
$1 != $2 { printf("=== Fields 1 and 2 not equal (NR=%s).n", NR) }
edited 3 hours ago
answered 3 hours ago
KusalanandaKusalananda
135k17255422
135k17255422
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%2f506180%2fhow-to-prevent-printing-to-stdout-from-an-awk-script-not-from-cli%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