How do I write awk '{print $1+$2+$3} file1 > file2 for n columns instead of 3?
awk '{ for (i = 1; i <= NF; i++) sum[i]+=$i} END{for (i in sum) print sum[i]}' file1 > file2
This helps sum all record-wise but a similar scheme wouldn't help do a column-wise sum (maybe). How to generalize column-wise addition to n columns?
cat file1
23 46 45
45 57 58
56 78 74
cat file2
114
160
208
text-processing awk
add a comment |
awk '{ for (i = 1; i <= NF; i++) sum[i]+=$i} END{for (i in sum) print sum[i]}' file1 > file2
This helps sum all record-wise but a similar scheme wouldn't help do a column-wise sum (maybe). How to generalize column-wise addition to n columns?
cat file1
23 46 45
45 57 58
56 78 74
cat file2
114
160
208
text-processing awk
add a comment |
awk '{ for (i = 1; i <= NF; i++) sum[i]+=$i} END{for (i in sum) print sum[i]}' file1 > file2
This helps sum all record-wise but a similar scheme wouldn't help do a column-wise sum (maybe). How to generalize column-wise addition to n columns?
cat file1
23 46 45
45 57 58
56 78 74
cat file2
114
160
208
text-processing awk
awk '{ for (i = 1; i <= NF; i++) sum[i]+=$i} END{for (i in sum) print sum[i]}' file1 > file2
This helps sum all record-wise but a similar scheme wouldn't help do a column-wise sum (maybe). How to generalize column-wise addition to n columns?
cat file1
23 46 45
45 57 58
56 78 74
cat file2
114
160
208
text-processing awk
text-processing awk
edited Feb 22 at 13:07
Rui F Ribeiro
41.2k1481140
41.2k1481140
asked Feb 19 at 17:59
Hitanshu SachaniaHitanshu Sachania
425
425
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You want to compute the sum of the fields for each record, so it's just:
awk '{sum = 0; for (i = 1; i <= NF; i++) sum += $i; print sum}' < file1 > file2
The curly braces begin an action statement that is executed on every line of the input; there is no preceding condition that would limit its execution to lines that satisfy such a condition.
On each line:
- Initialize a
sum
variable to zero. - Loop through the fields, starting at field #1 and ending at the last field (the special variable
NF
), and incrementsum
by the value of that field ($i
). - Print the value of the
sum
variable.
I tried this but it doesn't complete. The command keeps running with no change on the terminal.
– Hitanshu Sachania
Feb 19 at 18:10
@HitanshuSachania, you need to feed input to it, like by adding< file1
if that input is in thefile1
file.
– Stéphane Chazelas
Feb 19 at 18:11
Thank you, it worked. But I don't understand it, can you please explain it to me.
– Hitanshu Sachania
Feb 19 at 18:18
2
@HitanshuSachania if you don't understand that, then how were you able to write the version from your answer, which is more complicated? (Notice however that the(for i in sum)
is not guaranteed to iterate though the keys in any specific order (and it the realnawk
the order for the 1..100 keys will be something like 59,22,2,23,3,...,21,1).
– mosvy
Feb 19 at 18:51
2
@HitanshuSachania because awk arrays are usually implemented using a hash table and each implementation may use its own hash function, and may iterate through the keys in the order in which they're stored in the table + linked lists structure. Even withmawk
(the defaultawk
in debian) the following will print 21 first:awk 'BEGIN{a[1]=a[21]=1;for(i in a) print i}'
– mosvy
Feb 21 at 1:10
|
show 2 more comments
This will be a little slower than awk, but it's very concise:
perl -MList::Util=sum0 -lane 'print sum0(@F)' file1
It uses the sum0
function of the core List::Util module.
1
perl -lane 'my $s; $s += $_ for @F; print $s' < file1
– Rakesh Sharma
Feb 20 at 0:57
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%2f501659%2fhow-do-i-write-awk-print-123-file1-file2-for-n-columns-instead-of-3%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
You want to compute the sum of the fields for each record, so it's just:
awk '{sum = 0; for (i = 1; i <= NF; i++) sum += $i; print sum}' < file1 > file2
The curly braces begin an action statement that is executed on every line of the input; there is no preceding condition that would limit its execution to lines that satisfy such a condition.
On each line:
- Initialize a
sum
variable to zero. - Loop through the fields, starting at field #1 and ending at the last field (the special variable
NF
), and incrementsum
by the value of that field ($i
). - Print the value of the
sum
variable.
I tried this but it doesn't complete. The command keeps running with no change on the terminal.
– Hitanshu Sachania
Feb 19 at 18:10
@HitanshuSachania, you need to feed input to it, like by adding< file1
if that input is in thefile1
file.
– Stéphane Chazelas
Feb 19 at 18:11
Thank you, it worked. But I don't understand it, can you please explain it to me.
– Hitanshu Sachania
Feb 19 at 18:18
2
@HitanshuSachania if you don't understand that, then how were you able to write the version from your answer, which is more complicated? (Notice however that the(for i in sum)
is not guaranteed to iterate though the keys in any specific order (and it the realnawk
the order for the 1..100 keys will be something like 59,22,2,23,3,...,21,1).
– mosvy
Feb 19 at 18:51
2
@HitanshuSachania because awk arrays are usually implemented using a hash table and each implementation may use its own hash function, and may iterate through the keys in the order in which they're stored in the table + linked lists structure. Even withmawk
(the defaultawk
in debian) the following will print 21 first:awk 'BEGIN{a[1]=a[21]=1;for(i in a) print i}'
– mosvy
Feb 21 at 1:10
|
show 2 more comments
You want to compute the sum of the fields for each record, so it's just:
awk '{sum = 0; for (i = 1; i <= NF; i++) sum += $i; print sum}' < file1 > file2
The curly braces begin an action statement that is executed on every line of the input; there is no preceding condition that would limit its execution to lines that satisfy such a condition.
On each line:
- Initialize a
sum
variable to zero. - Loop through the fields, starting at field #1 and ending at the last field (the special variable
NF
), and incrementsum
by the value of that field ($i
). - Print the value of the
sum
variable.
I tried this but it doesn't complete. The command keeps running with no change on the terminal.
– Hitanshu Sachania
Feb 19 at 18:10
@HitanshuSachania, you need to feed input to it, like by adding< file1
if that input is in thefile1
file.
– Stéphane Chazelas
Feb 19 at 18:11
Thank you, it worked. But I don't understand it, can you please explain it to me.
– Hitanshu Sachania
Feb 19 at 18:18
2
@HitanshuSachania if you don't understand that, then how were you able to write the version from your answer, which is more complicated? (Notice however that the(for i in sum)
is not guaranteed to iterate though the keys in any specific order (and it the realnawk
the order for the 1..100 keys will be something like 59,22,2,23,3,...,21,1).
– mosvy
Feb 19 at 18:51
2
@HitanshuSachania because awk arrays are usually implemented using a hash table and each implementation may use its own hash function, and may iterate through the keys in the order in which they're stored in the table + linked lists structure. Even withmawk
(the defaultawk
in debian) the following will print 21 first:awk 'BEGIN{a[1]=a[21]=1;for(i in a) print i}'
– mosvy
Feb 21 at 1:10
|
show 2 more comments
You want to compute the sum of the fields for each record, so it's just:
awk '{sum = 0; for (i = 1; i <= NF; i++) sum += $i; print sum}' < file1 > file2
The curly braces begin an action statement that is executed on every line of the input; there is no preceding condition that would limit its execution to lines that satisfy such a condition.
On each line:
- Initialize a
sum
variable to zero. - Loop through the fields, starting at field #1 and ending at the last field (the special variable
NF
), and incrementsum
by the value of that field ($i
). - Print the value of the
sum
variable.
You want to compute the sum of the fields for each record, so it's just:
awk '{sum = 0; for (i = 1; i <= NF; i++) sum += $i; print sum}' < file1 > file2
The curly braces begin an action statement that is executed on every line of the input; there is no preceding condition that would limit its execution to lines that satisfy such a condition.
On each line:
- Initialize a
sum
variable to zero. - Loop through the fields, starting at field #1 and ending at the last field (the special variable
NF
), and incrementsum
by the value of that field ($i
). - Print the value of the
sum
variable.
edited Feb 19 at 19:18
answered Feb 19 at 18:02
Stéphane ChazelasStéphane Chazelas
309k57582942
309k57582942
I tried this but it doesn't complete. The command keeps running with no change on the terminal.
– Hitanshu Sachania
Feb 19 at 18:10
@HitanshuSachania, you need to feed input to it, like by adding< file1
if that input is in thefile1
file.
– Stéphane Chazelas
Feb 19 at 18:11
Thank you, it worked. But I don't understand it, can you please explain it to me.
– Hitanshu Sachania
Feb 19 at 18:18
2
@HitanshuSachania if you don't understand that, then how were you able to write the version from your answer, which is more complicated? (Notice however that the(for i in sum)
is not guaranteed to iterate though the keys in any specific order (and it the realnawk
the order for the 1..100 keys will be something like 59,22,2,23,3,...,21,1).
– mosvy
Feb 19 at 18:51
2
@HitanshuSachania because awk arrays are usually implemented using a hash table and each implementation may use its own hash function, and may iterate through the keys in the order in which they're stored in the table + linked lists structure. Even withmawk
(the defaultawk
in debian) the following will print 21 first:awk 'BEGIN{a[1]=a[21]=1;for(i in a) print i}'
– mosvy
Feb 21 at 1:10
|
show 2 more comments
I tried this but it doesn't complete. The command keeps running with no change on the terminal.
– Hitanshu Sachania
Feb 19 at 18:10
@HitanshuSachania, you need to feed input to it, like by adding< file1
if that input is in thefile1
file.
– Stéphane Chazelas
Feb 19 at 18:11
Thank you, it worked. But I don't understand it, can you please explain it to me.
– Hitanshu Sachania
Feb 19 at 18:18
2
@HitanshuSachania if you don't understand that, then how were you able to write the version from your answer, which is more complicated? (Notice however that the(for i in sum)
is not guaranteed to iterate though the keys in any specific order (and it the realnawk
the order for the 1..100 keys will be something like 59,22,2,23,3,...,21,1).
– mosvy
Feb 19 at 18:51
2
@HitanshuSachania because awk arrays are usually implemented using a hash table and each implementation may use its own hash function, and may iterate through the keys in the order in which they're stored in the table + linked lists structure. Even withmawk
(the defaultawk
in debian) the following will print 21 first:awk 'BEGIN{a[1]=a[21]=1;for(i in a) print i}'
– mosvy
Feb 21 at 1:10
I tried this but it doesn't complete. The command keeps running with no change on the terminal.
– Hitanshu Sachania
Feb 19 at 18:10
I tried this but it doesn't complete. The command keeps running with no change on the terminal.
– Hitanshu Sachania
Feb 19 at 18:10
@HitanshuSachania, you need to feed input to it, like by adding
< file1
if that input is in the file1
file.– Stéphane Chazelas
Feb 19 at 18:11
@HitanshuSachania, you need to feed input to it, like by adding
< file1
if that input is in the file1
file.– Stéphane Chazelas
Feb 19 at 18:11
Thank you, it worked. But I don't understand it, can you please explain it to me.
– Hitanshu Sachania
Feb 19 at 18:18
Thank you, it worked. But I don't understand it, can you please explain it to me.
– Hitanshu Sachania
Feb 19 at 18:18
2
2
@HitanshuSachania if you don't understand that, then how were you able to write the version from your answer, which is more complicated? (Notice however that the
(for i in sum)
is not guaranteed to iterate though the keys in any specific order (and it the real nawk
the order for the 1..100 keys will be something like 59,22,2,23,3,...,21,1).– mosvy
Feb 19 at 18:51
@HitanshuSachania if you don't understand that, then how were you able to write the version from your answer, which is more complicated? (Notice however that the
(for i in sum)
is not guaranteed to iterate though the keys in any specific order (and it the real nawk
the order for the 1..100 keys will be something like 59,22,2,23,3,...,21,1).– mosvy
Feb 19 at 18:51
2
2
@HitanshuSachania because awk arrays are usually implemented using a hash table and each implementation may use its own hash function, and may iterate through the keys in the order in which they're stored in the table + linked lists structure. Even with
mawk
(the default awk
in debian) the following will print 21 first: awk 'BEGIN{a[1]=a[21]=1;for(i in a) print i}'
– mosvy
Feb 21 at 1:10
@HitanshuSachania because awk arrays are usually implemented using a hash table and each implementation may use its own hash function, and may iterate through the keys in the order in which they're stored in the table + linked lists structure. Even with
mawk
(the default awk
in debian) the following will print 21 first: awk 'BEGIN{a[1]=a[21]=1;for(i in a) print i}'
– mosvy
Feb 21 at 1:10
|
show 2 more comments
This will be a little slower than awk, but it's very concise:
perl -MList::Util=sum0 -lane 'print sum0(@F)' file1
It uses the sum0
function of the core List::Util module.
1
perl -lane 'my $s; $s += $_ for @F; print $s' < file1
– Rakesh Sharma
Feb 20 at 0:57
add a comment |
This will be a little slower than awk, but it's very concise:
perl -MList::Util=sum0 -lane 'print sum0(@F)' file1
It uses the sum0
function of the core List::Util module.
1
perl -lane 'my $s; $s += $_ for @F; print $s' < file1
– Rakesh Sharma
Feb 20 at 0:57
add a comment |
This will be a little slower than awk, but it's very concise:
perl -MList::Util=sum0 -lane 'print sum0(@F)' file1
It uses the sum0
function of the core List::Util module.
This will be a little slower than awk, but it's very concise:
perl -MList::Util=sum0 -lane 'print sum0(@F)' file1
It uses the sum0
function of the core List::Util module.
answered Feb 19 at 20:04
glenn jackmanglenn jackman
52.2k572112
52.2k572112
1
perl -lane 'my $s; $s += $_ for @F; print $s' < file1
– Rakesh Sharma
Feb 20 at 0:57
add a comment |
1
perl -lane 'my $s; $s += $_ for @F; print $s' < file1
– Rakesh Sharma
Feb 20 at 0:57
1
1
perl -lane 'my $s; $s += $_ for @F; print $s' < file1
– Rakesh Sharma
Feb 20 at 0:57
perl -lane 'my $s; $s += $_ for @F; print $s' < file1
– Rakesh Sharma
Feb 20 at 0:57
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%2f501659%2fhow-do-i-write-awk-print-123-file1-file2-for-n-columns-instead-of-3%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