Sum and count in for loop
Clash Royale CLAN TAG#URR8PPP
I have five files each with a number in the first line.
I am attempting to write a bash script incorporating a for-loop that can calculate both the number (count) of the files and the sum of the numbers within in the files.
This is what I have attempted so far:
for file in $*
do
$[head -1 $file]
echo $(head -1)
done
I am unsure how to incorporate a sum and count element as yet.
shell-script files for
add a comment |
I have five files each with a number in the first line.
I am attempting to write a bash script incorporating a for-loop that can calculate both the number (count) of the files and the sum of the numbers within in the files.
This is what I have attempted so far:
for file in $*
do
$[head -1 $file]
echo $(head -1)
done
I am unsure how to incorporate a sum and count element as yet.
shell-script files for
Can you provide example context of file and expected output?
– Romeo Ninov
Feb 6 at 12:16
I am teaching myself for-loops. I have five files labeled file1,..,file5. The first entry of each file is a number. I would like to write a loop that a) adds all the numbers up and prints a sum b) reports the number of files it went through (ie the count).
– marzo
Feb 6 at 21:35
add a comment |
I have five files each with a number in the first line.
I am attempting to write a bash script incorporating a for-loop that can calculate both the number (count) of the files and the sum of the numbers within in the files.
This is what I have attempted so far:
for file in $*
do
$[head -1 $file]
echo $(head -1)
done
I am unsure how to incorporate a sum and count element as yet.
shell-script files for
I have five files each with a number in the first line.
I am attempting to write a bash script incorporating a for-loop that can calculate both the number (count) of the files and the sum of the numbers within in the files.
This is what I have attempted so far:
for file in $*
do
$[head -1 $file]
echo $(head -1)
done
I am unsure how to incorporate a sum and count element as yet.
shell-script files for
shell-script files for
asked Feb 6 at 11:47
marzomarzo
132
132
Can you provide example context of file and expected output?
– Romeo Ninov
Feb 6 at 12:16
I am teaching myself for-loops. I have five files labeled file1,..,file5. The first entry of each file is a number. I would like to write a loop that a) adds all the numbers up and prints a sum b) reports the number of files it went through (ie the count).
– marzo
Feb 6 at 21:35
add a comment |
Can you provide example context of file and expected output?
– Romeo Ninov
Feb 6 at 12:16
I am teaching myself for-loops. I have five files labeled file1,..,file5. The first entry of each file is a number. I would like to write a loop that a) adds all the numbers up and prints a sum b) reports the number of files it went through (ie the count).
– marzo
Feb 6 at 21:35
Can you provide example context of file and expected output?
– Romeo Ninov
Feb 6 at 12:16
Can you provide example context of file and expected output?
– Romeo Ninov
Feb 6 at 12:16
I am teaching myself for-loops. I have five files labeled file1,..,file5. The first entry of each file is a number. I would like to write a loop that a) adds all the numbers up and prints a sum b) reports the number of files it went through (ie the count).
– marzo
Feb 6 at 21:35
I am teaching myself for-loops. I have five files labeled file1,..,file5. The first entry of each file is a number. I would like to write a loop that a) adds all the numbers up and prints a sum b) reports the number of files it went through (ie the count).
– marzo
Feb 6 at 21:35
add a comment |
2 Answers
2
active
oldest
votes
I guess what you are trying to do can be achieved by this:
#!/bin/bash
sum=0
count=0
for file in "$@"
do
number=$(head -1 "$file")
count=$((count + 1))
sum=$((sum + number))
done
echo "The sum of the $count file(s) is: $sum"
You just add variables to store the sum
and the count
of the files you are working with. You then increase the count
each time you run through the loop. Also we add the number
at the beginning line of the file to our sum
-variable.
add a comment |
You could also do this with awk, avoiding the need to for head
for each file:
gawk 'count += 1; sum += $1; nextfile
END printf "count: %dt sum: %dn", count, sum' *
The first rule increments the count and sum, and then jumps to the next file, thus ignoring all but the first line of each file. In the END
, we print out the numbers.
nextfile
is a GNU thing, it might not work in other versions of awk
. Another alternative would be to explicitly work only on the first line:
awk 'FNR == 1 count += 1; sum += $1
END printf "count: %dt sum: %dn", count, sum' *
This would however read the files in full, even if it doesn't do anything with the rest of the lines.
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%2f499027%2fsum-and-count-in-for-loop%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
I guess what you are trying to do can be achieved by this:
#!/bin/bash
sum=0
count=0
for file in "$@"
do
number=$(head -1 "$file")
count=$((count + 1))
sum=$((sum + number))
done
echo "The sum of the $count file(s) is: $sum"
You just add variables to store the sum
and the count
of the files you are working with. You then increase the count
each time you run through the loop. Also we add the number
at the beginning line of the file to our sum
-variable.
add a comment |
I guess what you are trying to do can be achieved by this:
#!/bin/bash
sum=0
count=0
for file in "$@"
do
number=$(head -1 "$file")
count=$((count + 1))
sum=$((sum + number))
done
echo "The sum of the $count file(s) is: $sum"
You just add variables to store the sum
and the count
of the files you are working with. You then increase the count
each time you run through the loop. Also we add the number
at the beginning line of the file to our sum
-variable.
add a comment |
I guess what you are trying to do can be achieved by this:
#!/bin/bash
sum=0
count=0
for file in "$@"
do
number=$(head -1 "$file")
count=$((count + 1))
sum=$((sum + number))
done
echo "The sum of the $count file(s) is: $sum"
You just add variables to store the sum
and the count
of the files you are working with. You then increase the count
each time you run through the loop. Also we add the number
at the beginning line of the file to our sum
-variable.
I guess what you are trying to do can be achieved by this:
#!/bin/bash
sum=0
count=0
for file in "$@"
do
number=$(head -1 "$file")
count=$((count + 1))
sum=$((sum + number))
done
echo "The sum of the $count file(s) is: $sum"
You just add variables to store the sum
and the count
of the files you are working with. You then increase the count
each time you run through the loop. Also we add the number
at the beginning line of the file to our sum
-variable.
edited Feb 6 at 12:55
ilkkachu
60.1k998171
60.1k998171
answered Feb 6 at 12:48
majesticLSDmajesticLSD
763
763
add a comment |
add a comment |
You could also do this with awk, avoiding the need to for head
for each file:
gawk 'count += 1; sum += $1; nextfile
END printf "count: %dt sum: %dn", count, sum' *
The first rule increments the count and sum, and then jumps to the next file, thus ignoring all but the first line of each file. In the END
, we print out the numbers.
nextfile
is a GNU thing, it might not work in other versions of awk
. Another alternative would be to explicitly work only on the first line:
awk 'FNR == 1 count += 1; sum += $1
END printf "count: %dt sum: %dn", count, sum' *
This would however read the files in full, even if it doesn't do anything with the rest of the lines.
add a comment |
You could also do this with awk, avoiding the need to for head
for each file:
gawk 'count += 1; sum += $1; nextfile
END printf "count: %dt sum: %dn", count, sum' *
The first rule increments the count and sum, and then jumps to the next file, thus ignoring all but the first line of each file. In the END
, we print out the numbers.
nextfile
is a GNU thing, it might not work in other versions of awk
. Another alternative would be to explicitly work only on the first line:
awk 'FNR == 1 count += 1; sum += $1
END printf "count: %dt sum: %dn", count, sum' *
This would however read the files in full, even if it doesn't do anything with the rest of the lines.
add a comment |
You could also do this with awk, avoiding the need to for head
for each file:
gawk 'count += 1; sum += $1; nextfile
END printf "count: %dt sum: %dn", count, sum' *
The first rule increments the count and sum, and then jumps to the next file, thus ignoring all but the first line of each file. In the END
, we print out the numbers.
nextfile
is a GNU thing, it might not work in other versions of awk
. Another alternative would be to explicitly work only on the first line:
awk 'FNR == 1 count += 1; sum += $1
END printf "count: %dt sum: %dn", count, sum' *
This would however read the files in full, even if it doesn't do anything with the rest of the lines.
You could also do this with awk, avoiding the need to for head
for each file:
gawk 'count += 1; sum += $1; nextfile
END printf "count: %dt sum: %dn", count, sum' *
The first rule increments the count and sum, and then jumps to the next file, thus ignoring all but the first line of each file. In the END
, we print out the numbers.
nextfile
is a GNU thing, it might not work in other versions of awk
. Another alternative would be to explicitly work only on the first line:
awk 'FNR == 1 count += 1; sum += $1
END printf "count: %dt sum: %dn", count, sum' *
This would however read the files in full, even if it doesn't do anything with the rest of the lines.
edited Feb 6 at 13:28
Kusalananda
133k17253416
133k17253416
answered Feb 6 at 13:00
ilkkachuilkkachu
60.1k998171
60.1k998171
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%2f499027%2fsum-and-count-in-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
Can you provide example context of file and expected output?
– Romeo Ninov
Feb 6 at 12:16
I am teaching myself for-loops. I have five files labeled file1,..,file5. The first entry of each file is a number. I would like to write a loop that a) adds all the numbers up and prints a sum b) reports the number of files it went through (ie the count).
– marzo
Feb 6 at 21:35