Sum the values in a column except the header
I have a file as given below
--------------------------------------------------------------
Name_Customer Item_Purchased Item_Amount Credit
--------------------------------------------------------------
Tom H1_P 7657 N/A
Pras Track_1 23 N/A
Cha Brace 9 N/A
Moh kite37 269 N/A
Prab Bols 87699 N/A
I need to add the values under the column Item_Amount
by ignoring the header in the file and print the sum as
Total Amount collected = 95657
text-processing ksh columns
add a comment |
I have a file as given below
--------------------------------------------------------------
Name_Customer Item_Purchased Item_Amount Credit
--------------------------------------------------------------
Tom H1_P 7657 N/A
Pras Track_1 23 N/A
Cha Brace 9 N/A
Moh kite37 269 N/A
Prab Bols 87699 N/A
I need to add the values under the column Item_Amount
by ignoring the header in the file and print the sum as
Total Amount collected = 95657
text-processing ksh columns
add a comment |
I have a file as given below
--------------------------------------------------------------
Name_Customer Item_Purchased Item_Amount Credit
--------------------------------------------------------------
Tom H1_P 7657 N/A
Pras Track_1 23 N/A
Cha Brace 9 N/A
Moh kite37 269 N/A
Prab Bols 87699 N/A
I need to add the values under the column Item_Amount
by ignoring the header in the file and print the sum as
Total Amount collected = 95657
text-processing ksh columns
I have a file as given below
--------------------------------------------------------------
Name_Customer Item_Purchased Item_Amount Credit
--------------------------------------------------------------
Tom H1_P 7657 N/A
Pras Track_1 23 N/A
Cha Brace 9 N/A
Moh kite37 269 N/A
Prab Bols 87699 N/A
I need to add the values under the column Item_Amount
by ignoring the header in the file and print the sum as
Total Amount collected = 95657
text-processing ksh columns
text-processing ksh columns
edited Oct 14 '13 at 22:50
Gilles
534k12810781595
534k12810781595
asked Oct 14 '13 at 19:29
RamRam
1833411
1833411
add a comment |
add a comment |
7 Answers
7
active
oldest
votes
awk '{s+=$3}END{print s}' yourfile
@coffeMug can you explain why use this:s+0
?.
– Cold
Dec 15 '14 at 15:32
@Cold don’t remember exactly why that 0 is there! :-P Seems to be unnecessary so removed it!
– coffeMug
Dec 15 '14 at 19:43
@coffeMug probably to ensure awk treated the argument as a number
– Thorbjørn Ravn Andersen
May 9 '17 at 11:40
add a comment |
Pretty trivial using just awk
. Assuming the example data is in a file, ex.txt
:
$ awk '{total = total + int($3)}END{print "Total Amount collected = "total}' ex.txt
Example
$ awk '{total = total + $3}END{print "Total Amount collected = "total}' ex.txt
Total Amount collected = 95657
Details
Using awk
we collect the values from the 3rd column ($3
) and accumulate their sub-total in the variable total
. Once complete, as the last thing to do, END{..}
, we print the message along with the value of the variable total
.
just make one changesint($3)
– Rahul Patil
Oct 15 '13 at 6:45
@RahulPatil - thanks, changed it.
– slm♦
Oct 15 '13 at 6:47
add a comment |
The awk
approach is probably the easiest. Here are a few other choices:
Perl:
perl -lane '$k+=$F[2];END{print $k}' foo.txt
Pure coreutils:
t=0; tail -n +4 foo.txt | tr -s ' ' 't' | cut -d $'t' -f 3 |
while read i; do let t+=$i; echo $t; done | tail -n 1
add a comment |
If it can help:
grep -Eo '[0-9.]+' your_file|tr 'n' '+'|sed 's/+$//'|bc -l
add a comment |
total=0;
for n in $( tail -n +4 /tmp/reports.txt | awk '{print $3}') ;
do
total=$( expr $total + $n );
done ;
echo ">>$total"
1
You can't change the value of a variable inside a loop like that. See my answer here for a way to make variables accessible outside their loop.s
– terdon♦
Oct 14 '13 at 20:57
add a comment |
This pipeline should do the work:
tail -n +4 the_file | awk '{ sum += $3 } END { printf "Total Amount collected = %dn", sum }'
add a comment |
awk '{FS=","}{s+=$6}END{print s}' Filename
1
Can you explain it?
– P_Yadav
Jan 22 at 13:19
Welcome to U&L, this hardly add anything to accepted answer and other, besides I don't see the use of FS.
– Archemar
Jan 22 at 13:37
Given that there are no commas in the sample input, how does this work?
– Jeff Schaller
Jan 22 at 13:54
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%2f96031%2fsum-the-values-in-a-column-except-the-header%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
awk '{s+=$3}END{print s}' yourfile
@coffeMug can you explain why use this:s+0
?.
– Cold
Dec 15 '14 at 15:32
@Cold don’t remember exactly why that 0 is there! :-P Seems to be unnecessary so removed it!
– coffeMug
Dec 15 '14 at 19:43
@coffeMug probably to ensure awk treated the argument as a number
– Thorbjørn Ravn Andersen
May 9 '17 at 11:40
add a comment |
awk '{s+=$3}END{print s}' yourfile
@coffeMug can you explain why use this:s+0
?.
– Cold
Dec 15 '14 at 15:32
@Cold don’t remember exactly why that 0 is there! :-P Seems to be unnecessary so removed it!
– coffeMug
Dec 15 '14 at 19:43
@coffeMug probably to ensure awk treated the argument as a number
– Thorbjørn Ravn Andersen
May 9 '17 at 11:40
add a comment |
awk '{s+=$3}END{print s}' yourfile
awk '{s+=$3}END{print s}' yourfile
edited Dec 15 '14 at 19:44
answered Oct 14 '13 at 19:39
coffeMugcoffeMug
7,188112748
7,188112748
@coffeMug can you explain why use this:s+0
?.
– Cold
Dec 15 '14 at 15:32
@Cold don’t remember exactly why that 0 is there! :-P Seems to be unnecessary so removed it!
– coffeMug
Dec 15 '14 at 19:43
@coffeMug probably to ensure awk treated the argument as a number
– Thorbjørn Ravn Andersen
May 9 '17 at 11:40
add a comment |
@coffeMug can you explain why use this:s+0
?.
– Cold
Dec 15 '14 at 15:32
@Cold don’t remember exactly why that 0 is there! :-P Seems to be unnecessary so removed it!
– coffeMug
Dec 15 '14 at 19:43
@coffeMug probably to ensure awk treated the argument as a number
– Thorbjørn Ravn Andersen
May 9 '17 at 11:40
@coffeMug can you explain why use this:
s+0
?.– Cold
Dec 15 '14 at 15:32
@coffeMug can you explain why use this:
s+0
?.– Cold
Dec 15 '14 at 15:32
@Cold don’t remember exactly why that 0 is there! :-P Seems to be unnecessary so removed it!
– coffeMug
Dec 15 '14 at 19:43
@Cold don’t remember exactly why that 0 is there! :-P Seems to be unnecessary so removed it!
– coffeMug
Dec 15 '14 at 19:43
@coffeMug probably to ensure awk treated the argument as a number
– Thorbjørn Ravn Andersen
May 9 '17 at 11:40
@coffeMug probably to ensure awk treated the argument as a number
– Thorbjørn Ravn Andersen
May 9 '17 at 11:40
add a comment |
Pretty trivial using just awk
. Assuming the example data is in a file, ex.txt
:
$ awk '{total = total + int($3)}END{print "Total Amount collected = "total}' ex.txt
Example
$ awk '{total = total + $3}END{print "Total Amount collected = "total}' ex.txt
Total Amount collected = 95657
Details
Using awk
we collect the values from the 3rd column ($3
) and accumulate their sub-total in the variable total
. Once complete, as the last thing to do, END{..}
, we print the message along with the value of the variable total
.
just make one changesint($3)
– Rahul Patil
Oct 15 '13 at 6:45
@RahulPatil - thanks, changed it.
– slm♦
Oct 15 '13 at 6:47
add a comment |
Pretty trivial using just awk
. Assuming the example data is in a file, ex.txt
:
$ awk '{total = total + int($3)}END{print "Total Amount collected = "total}' ex.txt
Example
$ awk '{total = total + $3}END{print "Total Amount collected = "total}' ex.txt
Total Amount collected = 95657
Details
Using awk
we collect the values from the 3rd column ($3
) and accumulate their sub-total in the variable total
. Once complete, as the last thing to do, END{..}
, we print the message along with the value of the variable total
.
just make one changesint($3)
– Rahul Patil
Oct 15 '13 at 6:45
@RahulPatil - thanks, changed it.
– slm♦
Oct 15 '13 at 6:47
add a comment |
Pretty trivial using just awk
. Assuming the example data is in a file, ex.txt
:
$ awk '{total = total + int($3)}END{print "Total Amount collected = "total}' ex.txt
Example
$ awk '{total = total + $3}END{print "Total Amount collected = "total}' ex.txt
Total Amount collected = 95657
Details
Using awk
we collect the values from the 3rd column ($3
) and accumulate their sub-total in the variable total
. Once complete, as the last thing to do, END{..}
, we print the message along with the value of the variable total
.
Pretty trivial using just awk
. Assuming the example data is in a file, ex.txt
:
$ awk '{total = total + int($3)}END{print "Total Amount collected = "total}' ex.txt
Example
$ awk '{total = total + $3}END{print "Total Amount collected = "total}' ex.txt
Total Amount collected = 95657
Details
Using awk
we collect the values from the 3rd column ($3
) and accumulate their sub-total in the variable total
. Once complete, as the last thing to do, END{..}
, we print the message along with the value of the variable total
.
edited Oct 15 '13 at 6:47
answered Oct 15 '13 at 6:39
slm♦slm
249k66523682
249k66523682
just make one changesint($3)
– Rahul Patil
Oct 15 '13 at 6:45
@RahulPatil - thanks, changed it.
– slm♦
Oct 15 '13 at 6:47
add a comment |
just make one changesint($3)
– Rahul Patil
Oct 15 '13 at 6:45
@RahulPatil - thanks, changed it.
– slm♦
Oct 15 '13 at 6:47
just make one changes
int($3)
– Rahul Patil
Oct 15 '13 at 6:45
just make one changes
int($3)
– Rahul Patil
Oct 15 '13 at 6:45
@RahulPatil - thanks, changed it.
– slm♦
Oct 15 '13 at 6:47
@RahulPatil - thanks, changed it.
– slm♦
Oct 15 '13 at 6:47
add a comment |
The awk
approach is probably the easiest. Here are a few other choices:
Perl:
perl -lane '$k+=$F[2];END{print $k}' foo.txt
Pure coreutils:
t=0; tail -n +4 foo.txt | tr -s ' ' 't' | cut -d $'t' -f 3 |
while read i; do let t+=$i; echo $t; done | tail -n 1
add a comment |
The awk
approach is probably the easiest. Here are a few other choices:
Perl:
perl -lane '$k+=$F[2];END{print $k}' foo.txt
Pure coreutils:
t=0; tail -n +4 foo.txt | tr -s ' ' 't' | cut -d $'t' -f 3 |
while read i; do let t+=$i; echo $t; done | tail -n 1
add a comment |
The awk
approach is probably the easiest. Here are a few other choices:
Perl:
perl -lane '$k+=$F[2];END{print $k}' foo.txt
Pure coreutils:
t=0; tail -n +4 foo.txt | tr -s ' ' 't' | cut -d $'t' -f 3 |
while read i; do let t+=$i; echo $t; done | tail -n 1
The awk
approach is probably the easiest. Here are a few other choices:
Perl:
perl -lane '$k+=$F[2];END{print $k}' foo.txt
Pure coreutils:
t=0; tail -n +4 foo.txt | tr -s ' ' 't' | cut -d $'t' -f 3 |
while read i; do let t+=$i; echo $t; done | tail -n 1
edited Oct 14 '13 at 20:02
answered Oct 14 '13 at 19:50
terdon♦terdon
130k32254432
130k32254432
add a comment |
add a comment |
If it can help:
grep -Eo '[0-9.]+' your_file|tr 'n' '+'|sed 's/+$//'|bc -l
add a comment |
If it can help:
grep -Eo '[0-9.]+' your_file|tr 'n' '+'|sed 's/+$//'|bc -l
add a comment |
If it can help:
grep -Eo '[0-9.]+' your_file|tr 'n' '+'|sed 's/+$//'|bc -l
If it can help:
grep -Eo '[0-9.]+' your_file|tr 'n' '+'|sed 's/+$//'|bc -l
edited Apr 4 '18 at 15:01
datUser
2,5061133
2,5061133
answered Apr 4 '18 at 14:53
K-melK-mel
111
111
add a comment |
add a comment |
total=0;
for n in $( tail -n +4 /tmp/reports.txt | awk '{print $3}') ;
do
total=$( expr $total + $n );
done ;
echo ">>$total"
1
You can't change the value of a variable inside a loop like that. See my answer here for a way to make variables accessible outside their loop.s
– terdon♦
Oct 14 '13 at 20:57
add a comment |
total=0;
for n in $( tail -n +4 /tmp/reports.txt | awk '{print $3}') ;
do
total=$( expr $total + $n );
done ;
echo ">>$total"
1
You can't change the value of a variable inside a loop like that. See my answer here for a way to make variables accessible outside their loop.s
– terdon♦
Oct 14 '13 at 20:57
add a comment |
total=0;
for n in $( tail -n +4 /tmp/reports.txt | awk '{print $3}') ;
do
total=$( expr $total + $n );
done ;
echo ">>$total"
total=0;
for n in $( tail -n +4 /tmp/reports.txt | awk '{print $3}') ;
do
total=$( expr $total + $n );
done ;
echo ">>$total"
edited Oct 14 '13 at 19:50
terdon♦
130k32254432
130k32254432
answered Oct 14 '13 at 19:44
josejose
11
11
1
You can't change the value of a variable inside a loop like that. See my answer here for a way to make variables accessible outside their loop.s
– terdon♦
Oct 14 '13 at 20:57
add a comment |
1
You can't change the value of a variable inside a loop like that. See my answer here for a way to make variables accessible outside their loop.s
– terdon♦
Oct 14 '13 at 20:57
1
1
You can't change the value of a variable inside a loop like that. See my answer here for a way to make variables accessible outside their loop.s
– terdon♦
Oct 14 '13 at 20:57
You can't change the value of a variable inside a loop like that. See my answer here for a way to make variables accessible outside their loop.s
– terdon♦
Oct 14 '13 at 20:57
add a comment |
This pipeline should do the work:
tail -n +4 the_file | awk '{ sum += $3 } END { printf "Total Amount collected = %dn", sum }'
add a comment |
This pipeline should do the work:
tail -n +4 the_file | awk '{ sum += $3 } END { printf "Total Amount collected = %dn", sum }'
add a comment |
This pipeline should do the work:
tail -n +4 the_file | awk '{ sum += $3 } END { printf "Total Amount collected = %dn", sum }'
This pipeline should do the work:
tail -n +4 the_file | awk '{ sum += $3 } END { printf "Total Amount collected = %dn", sum }'
edited Oct 14 '13 at 22:43
answered Oct 14 '13 at 22:29
user13742
add a comment |
add a comment |
awk '{FS=","}{s+=$6}END{print s}' Filename
1
Can you explain it?
– P_Yadav
Jan 22 at 13:19
Welcome to U&L, this hardly add anything to accepted answer and other, besides I don't see the use of FS.
– Archemar
Jan 22 at 13:37
Given that there are no commas in the sample input, how does this work?
– Jeff Schaller
Jan 22 at 13:54
add a comment |
awk '{FS=","}{s+=$6}END{print s}' Filename
1
Can you explain it?
– P_Yadav
Jan 22 at 13:19
Welcome to U&L, this hardly add anything to accepted answer and other, besides I don't see the use of FS.
– Archemar
Jan 22 at 13:37
Given that there are no commas in the sample input, how does this work?
– Jeff Schaller
Jan 22 at 13:54
add a comment |
awk '{FS=","}{s+=$6}END{print s}' Filename
awk '{FS=","}{s+=$6}END{print s}' Filename
edited Jan 22 at 13:54
Jeff Schaller
40.6k1056129
40.6k1056129
answered Jan 22 at 13:14
Madhu KMadhu K
1
1
1
Can you explain it?
– P_Yadav
Jan 22 at 13:19
Welcome to U&L, this hardly add anything to accepted answer and other, besides I don't see the use of FS.
– Archemar
Jan 22 at 13:37
Given that there are no commas in the sample input, how does this work?
– Jeff Schaller
Jan 22 at 13:54
add a comment |
1
Can you explain it?
– P_Yadav
Jan 22 at 13:19
Welcome to U&L, this hardly add anything to accepted answer and other, besides I don't see the use of FS.
– Archemar
Jan 22 at 13:37
Given that there are no commas in the sample input, how does this work?
– Jeff Schaller
Jan 22 at 13:54
1
1
Can you explain it?
– P_Yadav
Jan 22 at 13:19
Can you explain it?
– P_Yadav
Jan 22 at 13:19
Welcome to U&L, this hardly add anything to accepted answer and other, besides I don't see the use of FS.
– Archemar
Jan 22 at 13:37
Welcome to U&L, this hardly add anything to accepted answer and other, besides I don't see the use of FS.
– Archemar
Jan 22 at 13:37
Given that there are no commas in the sample input, how does this work?
– Jeff Schaller
Jan 22 at 13:54
Given that there are no commas in the sample input, how does this work?
– Jeff Schaller
Jan 22 at 13:54
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%2f96031%2fsum-the-values-in-a-column-except-the-header%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