Awk command inside a for loop
I try, with no success, to use an awk command inside a for loop. I'v got a variable which contains a series of strings that I want to cut with awk to get the data. I know how to do that but what I really want is to cut the data successively.
So I've got this variable:
var="data1,data2,data3"
And here where I am right now:
for ((i=1; i<=3; i++))
do
echo $(awk -F, '{print $1}' <<<$var)
done
I try to replace the $1 by the loop $i but without success.
shell-script awk
New contributor
add a comment |
I try, with no success, to use an awk command inside a for loop. I'v got a variable which contains a series of strings that I want to cut with awk to get the data. I know how to do that but what I really want is to cut the data successively.
So I've got this variable:
var="data1,data2,data3"
And here where I am right now:
for ((i=1; i<=3; i++))
do
echo $(awk -F, '{print $1}' <<<$var)
done
I try to replace the $1 by the loop $i but without success.
shell-script awk
New contributor
Every character in between a pair of single quotes is treated as a literal character.
– Niko Gambt
28 mins ago
1
What is your real use-case? looping over awk seems unnecessary here (for exaple in bash you could use a parameter substitutionecho "${var//,/$'n'}"
)
– steeldriver
15 mins ago
add a comment |
I try, with no success, to use an awk command inside a for loop. I'v got a variable which contains a series of strings that I want to cut with awk to get the data. I know how to do that but what I really want is to cut the data successively.
So I've got this variable:
var="data1,data2,data3"
And here where I am right now:
for ((i=1; i<=3; i++))
do
echo $(awk -F, '{print $1}' <<<$var)
done
I try to replace the $1 by the loop $i but without success.
shell-script awk
New contributor
I try, with no success, to use an awk command inside a for loop. I'v got a variable which contains a series of strings that I want to cut with awk to get the data. I know how to do that but what I really want is to cut the data successively.
So I've got this variable:
var="data1,data2,data3"
And here where I am right now:
for ((i=1; i<=3; i++))
do
echo $(awk -F, '{print $1}' <<<$var)
done
I try to replace the $1 by the loop $i but without success.
shell-script awk
shell-script awk
New contributor
New contributor
edited 17 mins ago
Rui F Ribeiro
39.6k1479132
39.6k1479132
New contributor
asked 42 mins ago
AmargeinAmargein
61
61
New contributor
New contributor
Every character in between a pair of single quotes is treated as a literal character.
– Niko Gambt
28 mins ago
1
What is your real use-case? looping over awk seems unnecessary here (for exaple in bash you could use a parameter substitutionecho "${var//,/$'n'}"
)
– steeldriver
15 mins ago
add a comment |
Every character in between a pair of single quotes is treated as a literal character.
– Niko Gambt
28 mins ago
1
What is your real use-case? looping over awk seems unnecessary here (for exaple in bash you could use a parameter substitutionecho "${var//,/$'n'}"
)
– steeldriver
15 mins ago
Every character in between a pair of single quotes is treated as a literal character.
– Niko Gambt
28 mins ago
Every character in between a pair of single quotes is treated as a literal character.
– Niko Gambt
28 mins ago
1
1
What is your real use-case? looping over awk seems unnecessary here (for exaple in bash you could use a parameter substitution
echo "${var//,/$'n'}"
)– steeldriver
15 mins ago
What is your real use-case? looping over awk seems unnecessary here (for exaple in bash you could use a parameter substitution
echo "${var//,/$'n'}"
)– steeldriver
15 mins ago
add a comment |
3 Answers
3
active
oldest
votes
You can accomplish what you're trying to do by using double quotes in the awk script to inject the shell variable into it. You still want to keep one literal $
in it, which you can do by escaping it with backslash:
echo $(awk -F, "{print $$i}" <<<$var)
This will expand the $i
to 1
, 2
and 3
in each of the iterations, therefore awk will see $1
, $2
and $3
which will make it expand each of the fields.
Another possibility is to inject the shell variable as an awk variable using the -v
flag:
echo $(awk -F, -v i="$i" '{print $i}' <<<$var)
That injects the variable into awk with the same name. Variables in awk don't use a $
, which is used for fields, so $i
is enough to refer to the i-th field if i is a variable in awk.
Injecting a variable with -v
is generally a safer approach, particularly when it can contain arbitrary sequences of characters, in that case there's less risk that the contents will be executed as awk code against your intentions. But since in your case the variable holds a single integer, that's less of a concern.
Yet another option is to use a for
loop in awk itself. See awk documentation (or search this site) for more details on how to do that.
1
... or set the input record separator appropriatelyawk -v RS='[,n]' 1 <<< "$var"
(or maybe more portablyprintf "$var" | awk -v RS=, 1
)
– steeldriver
13 mins ago
@steeldriver That awk script will print all three fields at once. Which is ok given the OP is doing just that... I ended up focusing the answer on extracting a single field on the assumption they were interested in executing other commands in the shell loop (which might have been the motivation for using one in the first place...)
– filbranden
6 mins ago
1
Understood - that's why I commented on the OP to clarify what they really want to do ;)
– steeldriver
3 mins ago
add a comment |
Using awk
seems excessive in this circumstance, how about a tr
and a while-loop:
tr , 'n' <<<"$var" | while read; do
echo $REPLY
done
Output:
data1
data2
data3
add a comment |
awk can accept both j
(as variable) and $j
(as field index):
for i in [1-3]; do echo $var | awk -v j=$i -F , '{print $j}'; done
$i
in the example "confused" awk
which one to use (shell or its own variable - taking precedence) as both are referred to with $
prefix.
note
sh shell which is standard for "portable" scripting do not support the:
(( i=1; i<=3; i++; ))
and <<< $var
constructs
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
});
}
});
Amargein is a new contributor. Be nice, and check out our Code of Conduct.
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%2f496994%2fawk-command-inside-a-for-loop%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
You can accomplish what you're trying to do by using double quotes in the awk script to inject the shell variable into it. You still want to keep one literal $
in it, which you can do by escaping it with backslash:
echo $(awk -F, "{print $$i}" <<<$var)
This will expand the $i
to 1
, 2
and 3
in each of the iterations, therefore awk will see $1
, $2
and $3
which will make it expand each of the fields.
Another possibility is to inject the shell variable as an awk variable using the -v
flag:
echo $(awk -F, -v i="$i" '{print $i}' <<<$var)
That injects the variable into awk with the same name. Variables in awk don't use a $
, which is used for fields, so $i
is enough to refer to the i-th field if i is a variable in awk.
Injecting a variable with -v
is generally a safer approach, particularly when it can contain arbitrary sequences of characters, in that case there's less risk that the contents will be executed as awk code against your intentions. But since in your case the variable holds a single integer, that's less of a concern.
Yet another option is to use a for
loop in awk itself. See awk documentation (or search this site) for more details on how to do that.
1
... or set the input record separator appropriatelyawk -v RS='[,n]' 1 <<< "$var"
(or maybe more portablyprintf "$var" | awk -v RS=, 1
)
– steeldriver
13 mins ago
@steeldriver That awk script will print all three fields at once. Which is ok given the OP is doing just that... I ended up focusing the answer on extracting a single field on the assumption they were interested in executing other commands in the shell loop (which might have been the motivation for using one in the first place...)
– filbranden
6 mins ago
1
Understood - that's why I commented on the OP to clarify what they really want to do ;)
– steeldriver
3 mins ago
add a comment |
You can accomplish what you're trying to do by using double quotes in the awk script to inject the shell variable into it. You still want to keep one literal $
in it, which you can do by escaping it with backslash:
echo $(awk -F, "{print $$i}" <<<$var)
This will expand the $i
to 1
, 2
and 3
in each of the iterations, therefore awk will see $1
, $2
and $3
which will make it expand each of the fields.
Another possibility is to inject the shell variable as an awk variable using the -v
flag:
echo $(awk -F, -v i="$i" '{print $i}' <<<$var)
That injects the variable into awk with the same name. Variables in awk don't use a $
, which is used for fields, so $i
is enough to refer to the i-th field if i is a variable in awk.
Injecting a variable with -v
is generally a safer approach, particularly when it can contain arbitrary sequences of characters, in that case there's less risk that the contents will be executed as awk code against your intentions. But since in your case the variable holds a single integer, that's less of a concern.
Yet another option is to use a for
loop in awk itself. See awk documentation (or search this site) for more details on how to do that.
1
... or set the input record separator appropriatelyawk -v RS='[,n]' 1 <<< "$var"
(or maybe more portablyprintf "$var" | awk -v RS=, 1
)
– steeldriver
13 mins ago
@steeldriver That awk script will print all three fields at once. Which is ok given the OP is doing just that... I ended up focusing the answer on extracting a single field on the assumption they were interested in executing other commands in the shell loop (which might have been the motivation for using one in the first place...)
– filbranden
6 mins ago
1
Understood - that's why I commented on the OP to clarify what they really want to do ;)
– steeldriver
3 mins ago
add a comment |
You can accomplish what you're trying to do by using double quotes in the awk script to inject the shell variable into it. You still want to keep one literal $
in it, which you can do by escaping it with backslash:
echo $(awk -F, "{print $$i}" <<<$var)
This will expand the $i
to 1
, 2
and 3
in each of the iterations, therefore awk will see $1
, $2
and $3
which will make it expand each of the fields.
Another possibility is to inject the shell variable as an awk variable using the -v
flag:
echo $(awk -F, -v i="$i" '{print $i}' <<<$var)
That injects the variable into awk with the same name. Variables in awk don't use a $
, which is used for fields, so $i
is enough to refer to the i-th field if i is a variable in awk.
Injecting a variable with -v
is generally a safer approach, particularly when it can contain arbitrary sequences of characters, in that case there's less risk that the contents will be executed as awk code against your intentions. But since in your case the variable holds a single integer, that's less of a concern.
Yet another option is to use a for
loop in awk itself. See awk documentation (or search this site) for more details on how to do that.
You can accomplish what you're trying to do by using double quotes in the awk script to inject the shell variable into it. You still want to keep one literal $
in it, which you can do by escaping it with backslash:
echo $(awk -F, "{print $$i}" <<<$var)
This will expand the $i
to 1
, 2
and 3
in each of the iterations, therefore awk will see $1
, $2
and $3
which will make it expand each of the fields.
Another possibility is to inject the shell variable as an awk variable using the -v
flag:
echo $(awk -F, -v i="$i" '{print $i}' <<<$var)
That injects the variable into awk with the same name. Variables in awk don't use a $
, which is used for fields, so $i
is enough to refer to the i-th field if i is a variable in awk.
Injecting a variable with -v
is generally a safer approach, particularly when it can contain arbitrary sequences of characters, in that case there's less risk that the contents will be executed as awk code against your intentions. But since in your case the variable holds a single integer, that's less of a concern.
Yet another option is to use a for
loop in awk itself. See awk documentation (or search this site) for more details on how to do that.
edited 25 mins ago
answered 32 mins ago
filbrandenfilbranden
7,84521038
7,84521038
1
... or set the input record separator appropriatelyawk -v RS='[,n]' 1 <<< "$var"
(or maybe more portablyprintf "$var" | awk -v RS=, 1
)
– steeldriver
13 mins ago
@steeldriver That awk script will print all three fields at once. Which is ok given the OP is doing just that... I ended up focusing the answer on extracting a single field on the assumption they were interested in executing other commands in the shell loop (which might have been the motivation for using one in the first place...)
– filbranden
6 mins ago
1
Understood - that's why I commented on the OP to clarify what they really want to do ;)
– steeldriver
3 mins ago
add a comment |
1
... or set the input record separator appropriatelyawk -v RS='[,n]' 1 <<< "$var"
(or maybe more portablyprintf "$var" | awk -v RS=, 1
)
– steeldriver
13 mins ago
@steeldriver That awk script will print all three fields at once. Which is ok given the OP is doing just that... I ended up focusing the answer on extracting a single field on the assumption they were interested in executing other commands in the shell loop (which might have been the motivation for using one in the first place...)
– filbranden
6 mins ago
1
Understood - that's why I commented on the OP to clarify what they really want to do ;)
– steeldriver
3 mins ago
1
1
... or set the input record separator appropriately
awk -v RS='[,n]' 1 <<< "$var"
(or maybe more portably printf "$var" | awk -v RS=, 1
)– steeldriver
13 mins ago
... or set the input record separator appropriately
awk -v RS='[,n]' 1 <<< "$var"
(or maybe more portably printf "$var" | awk -v RS=, 1
)– steeldriver
13 mins ago
@steeldriver That awk script will print all three fields at once. Which is ok given the OP is doing just that... I ended up focusing the answer on extracting a single field on the assumption they were interested in executing other commands in the shell loop (which might have been the motivation for using one in the first place...)
– filbranden
6 mins ago
@steeldriver That awk script will print all three fields at once. Which is ok given the OP is doing just that... I ended up focusing the answer on extracting a single field on the assumption they were interested in executing other commands in the shell loop (which might have been the motivation for using one in the first place...)
– filbranden
6 mins ago
1
1
Understood - that's why I commented on the OP to clarify what they really want to do ;)
– steeldriver
3 mins ago
Understood - that's why I commented on the OP to clarify what they really want to do ;)
– steeldriver
3 mins ago
add a comment |
Using awk
seems excessive in this circumstance, how about a tr
and a while-loop:
tr , 'n' <<<"$var" | while read; do
echo $REPLY
done
Output:
data1
data2
data3
add a comment |
Using awk
seems excessive in this circumstance, how about a tr
and a while-loop:
tr , 'n' <<<"$var" | while read; do
echo $REPLY
done
Output:
data1
data2
data3
add a comment |
Using awk
seems excessive in this circumstance, how about a tr
and a while-loop:
tr , 'n' <<<"$var" | while read; do
echo $REPLY
done
Output:
data1
data2
data3
Using awk
seems excessive in this circumstance, how about a tr
and a while-loop:
tr , 'n' <<<"$var" | while read; do
echo $REPLY
done
Output:
data1
data2
data3
answered 4 mins ago
ThorThor
11.7k13459
11.7k13459
add a comment |
add a comment |
awk can accept both j
(as variable) and $j
(as field index):
for i in [1-3]; do echo $var | awk -v j=$i -F , '{print $j}'; done
$i
in the example "confused" awk
which one to use (shell or its own variable - taking precedence) as both are referred to with $
prefix.
note
sh shell which is standard for "portable" scripting do not support the:
(( i=1; i<=3; i++; ))
and <<< $var
constructs
add a comment |
awk can accept both j
(as variable) and $j
(as field index):
for i in [1-3]; do echo $var | awk -v j=$i -F , '{print $j}'; done
$i
in the example "confused" awk
which one to use (shell or its own variable - taking precedence) as both are referred to with $
prefix.
note
sh shell which is standard for "portable" scripting do not support the:
(( i=1; i<=3; i++; ))
and <<< $var
constructs
add a comment |
awk can accept both j
(as variable) and $j
(as field index):
for i in [1-3]; do echo $var | awk -v j=$i -F , '{print $j}'; done
$i
in the example "confused" awk
which one to use (shell or its own variable - taking precedence) as both are referred to with $
prefix.
note
sh shell which is standard for "portable" scripting do not support the:
(( i=1; i<=3; i++; ))
and <<< $var
constructs
awk can accept both j
(as variable) and $j
(as field index):
for i in [1-3]; do echo $var | awk -v j=$i -F , '{print $j}'; done
$i
in the example "confused" awk
which one to use (shell or its own variable - taking precedence) as both are referred to with $
prefix.
note
sh shell which is standard for "portable" scripting do not support the:
(( i=1; i<=3; i++; ))
and <<< $var
constructs
edited 4 mins ago
answered 13 mins ago
w17tw17t
676522
676522
add a comment |
add a comment |
Amargein is a new contributor. Be nice, and check out our Code of Conduct.
Amargein is a new contributor. Be nice, and check out our Code of Conduct.
Amargein is a new contributor. Be nice, and check out our Code of Conduct.
Amargein is a new contributor. Be nice, and check out our Code of Conduct.
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%2f496994%2fawk-command-inside-a-for-loop%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
Every character in between a pair of single quotes is treated as a literal character.
– Niko Gambt
28 mins ago
1
What is your real use-case? looping over awk seems unnecessary here (for exaple in bash you could use a parameter substitution
echo "${var//,/$'n'}"
)– steeldriver
15 mins ago