how to set a variable to multiple values with awk?
I have a file similar to the following :
x 0
1 x
1 1
and I am essentially trying to see if both fields are equal to one another or not for every row. But the problem is that this file contains x which can either hold the value of 0 or 1 -- but I am not sure how to set this in awk
I tried working with the following code that did not work, it only works if x is set to either 0 or 1, not both.
y=$(seq 1 2)
awk -v x="$y" '{ if ($1==$2) print "good" }' file
Please advise or let me know if any clarification is necessary, thank you.
awk
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I have a file similar to the following :
x 0
1 x
1 1
and I am essentially trying to see if both fields are equal to one another or not for every row. But the problem is that this file contains x which can either hold the value of 0 or 1 -- but I am not sure how to set this in awk
I tried working with the following code that did not work, it only works if x is set to either 0 or 1, not both.
y=$(seq 1 2)
awk -v x="$y" '{ if ($1==$2) print "good" }' file
Please advise or let me know if any clarification is necessary, thank you.
awk
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I have a file similar to the following :
x 0
1 x
1 1
and I am essentially trying to see if both fields are equal to one another or not for every row. But the problem is that this file contains x which can either hold the value of 0 or 1 -- but I am not sure how to set this in awk
I tried working with the following code that did not work, it only works if x is set to either 0 or 1, not both.
y=$(seq 1 2)
awk -v x="$y" '{ if ($1==$2) print "good" }' file
Please advise or let me know if any clarification is necessary, thank you.
awk
I have a file similar to the following :
x 0
1 x
1 1
and I am essentially trying to see if both fields are equal to one another or not for every row. But the problem is that this file contains x which can either hold the value of 0 or 1 -- but I am not sure how to set this in awk
I tried working with the following code that did not work, it only works if x is set to either 0 or 1, not both.
y=$(seq 1 2)
awk -v x="$y" '{ if ($1==$2) print "good" }' file
Please advise or let me know if any clarification is necessary, thank you.
awk
awk
asked Nov 26 '15 at 20:39
anontanont
112
112
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
A scalar variable in awk can only hold a single variable. You'll have to do something like this:
if (
($1 == "x" && ($2 == 0 || $2 == 1)) ||
($2 == "x" && ($1 == 0 || $1 == 1)) ||
$1 == $2
) print "good"
add a comment |
You can't set a variable to multiple values in any ordinary programming language that I know of, but you may most certainly loop over a set of values with a variable.
Construct a shell loop around the awk code that runs for each value of x:
for x in 0 1; do
printf 'Running with x=%dn' "$x"
awk -v x="$x"
'$1 == "x" { $1 = x }
$2 == "x" { $2 = x }
$1 == $2 { printf("match on line %d: %d == %dn", NR, $1, $2) }' file.in
done
The awk code tests the two columns for the character x and (if true) sets it to the current value of the awk variable x (which is set on the command line).
If the columns later are the same, some output is produced.
With the given data, this produces the output
Running with x=0
match on line 1: 0 == 0
match on line 3: 1 == 1
Running with x=1
match on line 2: 1 == 1
match on line 3: 1 == 1
Alternatively, move the loop into awk (which doesn't look as nice):
awk -v x="$x"
'{ for (x=0; x<=1; ++x) {
printf("x is %dn", x);
if ($1 == "x") { a = x } else { a = $1 }
if ($2 == "x") { b = x } else { b = $2 }
if (a == b) { printf("match on line %d: %d == %dn", NR, a, b) }
}
}' file.in
Output:
x is 0
match on line 1: 0 == 0
x is 1
x is 0
x is 1
match on line 2: 1 == 1
x is 0
match on line 3: 1 == 1
x is 1
match on line 3: 1 == 1
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%2f245742%2fhow-to-set-a-variable-to-multiple-values-with-awk%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
A scalar variable in awk can only hold a single variable. You'll have to do something like this:
if (
($1 == "x" && ($2 == 0 || $2 == 1)) ||
($2 == "x" && ($1 == 0 || $1 == 1)) ||
$1 == $2
) print "good"
add a comment |
A scalar variable in awk can only hold a single variable. You'll have to do something like this:
if (
($1 == "x" && ($2 == 0 || $2 == 1)) ||
($2 == "x" && ($1 == 0 || $1 == 1)) ||
$1 == $2
) print "good"
add a comment |
A scalar variable in awk can only hold a single variable. You'll have to do something like this:
if (
($1 == "x" && ($2 == 0 || $2 == 1)) ||
($2 == "x" && ($1 == 0 || $1 == 1)) ||
$1 == $2
) print "good"
A scalar variable in awk can only hold a single variable. You'll have to do something like this:
if (
($1 == "x" && ($2 == 0 || $2 == 1)) ||
($2 == "x" && ($1 == 0 || $1 == 1)) ||
$1 == $2
) print "good"
answered Nov 26 '15 at 21:03
glenn jackmanglenn jackman
51.2k571110
51.2k571110
add a comment |
add a comment |
You can't set a variable to multiple values in any ordinary programming language that I know of, but you may most certainly loop over a set of values with a variable.
Construct a shell loop around the awk code that runs for each value of x:
for x in 0 1; do
printf 'Running with x=%dn' "$x"
awk -v x="$x"
'$1 == "x" { $1 = x }
$2 == "x" { $2 = x }
$1 == $2 { printf("match on line %d: %d == %dn", NR, $1, $2) }' file.in
done
The awk code tests the two columns for the character x and (if true) sets it to the current value of the awk variable x (which is set on the command line).
If the columns later are the same, some output is produced.
With the given data, this produces the output
Running with x=0
match on line 1: 0 == 0
match on line 3: 1 == 1
Running with x=1
match on line 2: 1 == 1
match on line 3: 1 == 1
Alternatively, move the loop into awk (which doesn't look as nice):
awk -v x="$x"
'{ for (x=0; x<=1; ++x) {
printf("x is %dn", x);
if ($1 == "x") { a = x } else { a = $1 }
if ($2 == "x") { b = x } else { b = $2 }
if (a == b) { printf("match on line %d: %d == %dn", NR, a, b) }
}
}' file.in
Output:
x is 0
match on line 1: 0 == 0
x is 1
x is 0
x is 1
match on line 2: 1 == 1
x is 0
match on line 3: 1 == 1
x is 1
match on line 3: 1 == 1
add a comment |
You can't set a variable to multiple values in any ordinary programming language that I know of, but you may most certainly loop over a set of values with a variable.
Construct a shell loop around the awk code that runs for each value of x:
for x in 0 1; do
printf 'Running with x=%dn' "$x"
awk -v x="$x"
'$1 == "x" { $1 = x }
$2 == "x" { $2 = x }
$1 == $2 { printf("match on line %d: %d == %dn", NR, $1, $2) }' file.in
done
The awk code tests the two columns for the character x and (if true) sets it to the current value of the awk variable x (which is set on the command line).
If the columns later are the same, some output is produced.
With the given data, this produces the output
Running with x=0
match on line 1: 0 == 0
match on line 3: 1 == 1
Running with x=1
match on line 2: 1 == 1
match on line 3: 1 == 1
Alternatively, move the loop into awk (which doesn't look as nice):
awk -v x="$x"
'{ for (x=0; x<=1; ++x) {
printf("x is %dn", x);
if ($1 == "x") { a = x } else { a = $1 }
if ($2 == "x") { b = x } else { b = $2 }
if (a == b) { printf("match on line %d: %d == %dn", NR, a, b) }
}
}' file.in
Output:
x is 0
match on line 1: 0 == 0
x is 1
x is 0
x is 1
match on line 2: 1 == 1
x is 0
match on line 3: 1 == 1
x is 1
match on line 3: 1 == 1
add a comment |
You can't set a variable to multiple values in any ordinary programming language that I know of, but you may most certainly loop over a set of values with a variable.
Construct a shell loop around the awk code that runs for each value of x:
for x in 0 1; do
printf 'Running with x=%dn' "$x"
awk -v x="$x"
'$1 == "x" { $1 = x }
$2 == "x" { $2 = x }
$1 == $2 { printf("match on line %d: %d == %dn", NR, $1, $2) }' file.in
done
The awk code tests the two columns for the character x and (if true) sets it to the current value of the awk variable x (which is set on the command line).
If the columns later are the same, some output is produced.
With the given data, this produces the output
Running with x=0
match on line 1: 0 == 0
match on line 3: 1 == 1
Running with x=1
match on line 2: 1 == 1
match on line 3: 1 == 1
Alternatively, move the loop into awk (which doesn't look as nice):
awk -v x="$x"
'{ for (x=0; x<=1; ++x) {
printf("x is %dn", x);
if ($1 == "x") { a = x } else { a = $1 }
if ($2 == "x") { b = x } else { b = $2 }
if (a == b) { printf("match on line %d: %d == %dn", NR, a, b) }
}
}' file.in
Output:
x is 0
match on line 1: 0 == 0
x is 1
x is 0
x is 1
match on line 2: 1 == 1
x is 0
match on line 3: 1 == 1
x is 1
match on line 3: 1 == 1
You can't set a variable to multiple values in any ordinary programming language that I know of, but you may most certainly loop over a set of values with a variable.
Construct a shell loop around the awk code that runs for each value of x:
for x in 0 1; do
printf 'Running with x=%dn' "$x"
awk -v x="$x"
'$1 == "x" { $1 = x }
$2 == "x" { $2 = x }
$1 == $2 { printf("match on line %d: %d == %dn", NR, $1, $2) }' file.in
done
The awk code tests the two columns for the character x and (if true) sets it to the current value of the awk variable x (which is set on the command line).
If the columns later are the same, some output is produced.
With the given data, this produces the output
Running with x=0
match on line 1: 0 == 0
match on line 3: 1 == 1
Running with x=1
match on line 2: 1 == 1
match on line 3: 1 == 1
Alternatively, move the loop into awk (which doesn't look as nice):
awk -v x="$x"
'{ for (x=0; x<=1; ++x) {
printf("x is %dn", x);
if ($1 == "x") { a = x } else { a = $1 }
if ($2 == "x") { b = x } else { b = $2 }
if (a == b) { printf("match on line %d: %d == %dn", NR, a, b) }
}
}' file.in
Output:
x is 0
match on line 1: 0 == 0
x is 1
x is 0
x is 1
match on line 2: 1 == 1
x is 0
match on line 3: 1 == 1
x is 1
match on line 3: 1 == 1
edited Sep 20 '17 at 9:08
answered Sep 19 '17 at 22:07
KusalanandaKusalananda
127k16239393
127k16239393
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%2f245742%2fhow-to-set-a-variable-to-multiple-values-with-awk%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