How do you calculate sum of a column without creating temp files in shell script?











up vote
1
down vote

favorite












given an input file with data like this:



id,name
10,abc
20,xyz
30,def


I am trying to sum the values in the id column.



Note: the reason I am using awk -v header is because I have multiple files all of which have id as a common header name but at different positions.



The output I am expecting is the total sum of id (i.e., for the example above the output is 60).



The code below works and returns the expected output, but I have to create a temp file in the code and then calculate the sum.



I tried many variations unfortunately all my efforts failed.



I want to avoid writing the data to a temp file, intfile.txt, but I am stuck.



Any solutions/suggestions appreciated.



ps: I am relatively new to shell scripting and I know code is not written well, but I am working through it.



#!/bin/bash
awk -v header="id" '
BEGIN { FS=","; a=0 }
NR == 1 { for (i=1;i<=NF;i++) { if ($i==header) { a=i }} }
a=NR > 1 && a>0 { print $a }' testfile.txt>intfile.txt
awk '{s+=$1}END{print s}' intfile.txt









share|improve this question




















  • 2




    Embedding a big awk script is hardly doing things "in shell script" -- you might as well have your shell script just call a Python interpreter. :)
    – Charles Duffy
    Nov 8 at 0:56

















up vote
1
down vote

favorite












given an input file with data like this:



id,name
10,abc
20,xyz
30,def


I am trying to sum the values in the id column.



Note: the reason I am using awk -v header is because I have multiple files all of which have id as a common header name but at different positions.



The output I am expecting is the total sum of id (i.e., for the example above the output is 60).



The code below works and returns the expected output, but I have to create a temp file in the code and then calculate the sum.



I tried many variations unfortunately all my efforts failed.



I want to avoid writing the data to a temp file, intfile.txt, but I am stuck.



Any solutions/suggestions appreciated.



ps: I am relatively new to shell scripting and I know code is not written well, but I am working through it.



#!/bin/bash
awk -v header="id" '
BEGIN { FS=","; a=0 }
NR == 1 { for (i=1;i<=NF;i++) { if ($i==header) { a=i }} }
a=NR > 1 && a>0 { print $a }' testfile.txt>intfile.txt
awk '{s+=$1}END{print s}' intfile.txt









share|improve this question




















  • 2




    Embedding a big awk script is hardly doing things "in shell script" -- you might as well have your shell script just call a Python interpreter. :)
    – Charles Duffy
    Nov 8 at 0:56















up vote
1
down vote

favorite









up vote
1
down vote

favorite











given an input file with data like this:



id,name
10,abc
20,xyz
30,def


I am trying to sum the values in the id column.



Note: the reason I am using awk -v header is because I have multiple files all of which have id as a common header name but at different positions.



The output I am expecting is the total sum of id (i.e., for the example above the output is 60).



The code below works and returns the expected output, but I have to create a temp file in the code and then calculate the sum.



I tried many variations unfortunately all my efforts failed.



I want to avoid writing the data to a temp file, intfile.txt, but I am stuck.



Any solutions/suggestions appreciated.



ps: I am relatively new to shell scripting and I know code is not written well, but I am working through it.



#!/bin/bash
awk -v header="id" '
BEGIN { FS=","; a=0 }
NR == 1 { for (i=1;i<=NF;i++) { if ($i==header) { a=i }} }
a=NR > 1 && a>0 { print $a }' testfile.txt>intfile.txt
awk '{s+=$1}END{print s}' intfile.txt









share|improve this question















given an input file with data like this:



id,name
10,abc
20,xyz
30,def


I am trying to sum the values in the id column.



Note: the reason I am using awk -v header is because I have multiple files all of which have id as a common header name but at different positions.



The output I am expecting is the total sum of id (i.e., for the example above the output is 60).



The code below works and returns the expected output, but I have to create a temp file in the code and then calculate the sum.



I tried many variations unfortunately all my efforts failed.



I want to avoid writing the data to a temp file, intfile.txt, but I am stuck.



Any solutions/suggestions appreciated.



ps: I am relatively new to shell scripting and I know code is not written well, but I am working through it.



#!/bin/bash
awk -v header="id" '
BEGIN { FS=","; a=0 }
NR == 1 { for (i=1;i<=NF;i++) { if ($i==header) { a=i }} }
a=NR > 1 && a>0 { print $a }' testfile.txt>intfile.txt
awk '{s+=$1}END{print s}' intfile.txt






shell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 0:47









David Yates

650823




650823










asked Nov 8 at 0:23









kumarm

64




64








  • 2




    Embedding a big awk script is hardly doing things "in shell script" -- you might as well have your shell script just call a Python interpreter. :)
    – Charles Duffy
    Nov 8 at 0:56
















  • 2




    Embedding a big awk script is hardly doing things "in shell script" -- you might as well have your shell script just call a Python interpreter. :)
    – Charles Duffy
    Nov 8 at 0:56










2




2




Embedding a big awk script is hardly doing things "in shell script" -- you might as well have your shell script just call a Python interpreter. :)
– Charles Duffy
Nov 8 at 0:56






Embedding a big awk script is hardly doing things "in shell script" -- you might as well have your shell script just call a Python interpreter. :)
– Charles Duffy
Nov 8 at 0:56














2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










#!/usr/bin/env bash

target="id" # the field you want to sum
target_idx= # the column number of that field
sum=0 # the sum that was found so far

{
# first, just read the header...
IFS=, read -r -a header
for idx in "${!header[@]}"; do # and look for the target field in it
[[ ${header[$idx]} = $target ]] && { target_idx=$idx; break; }
done
[[ $target_idx ]] || { echo "ERROR: No $target field found" >&2; exit 1; }

# then, iterate over other lines
while IFS=, read -r -a line; do
sum=$(( sum + ${line[$target_idx]} ))
done
} <testfile.txt

echo "$sum"


See this running at https://ideone.com/MOnpFM





Some references:




  • The basics of reading a file line-by-line by calling read repeatedly are covered in BashFAQ #1.

  • Fancy array syntax (including the parameter expansion "${!array[@]}" to iterate over indices) is covered in the bash-hackers' page on arrays, BashFAQ #5, and also the BashGuide array page.

  • Arithmetic syntax ($(( ... )) for math) is covered in the bash-hackers' wiki at http://wiki.bash-hackers.org/syntax/arith_expr






share|improve this answer























  • Thank-you for your time. quick question how do I pass my input file name to the script mentioned
    – kumarm
    Nov 8 at 1:02










  • Personally, I'd just take out the <testfile.txt from inside the script and connect the input to stdin; thus, invoking it as ./yourscript <"$realInputFileName" (assuming realInputFileName is a variable that holds, well, the input file's name).
    – Charles Duffy
    Nov 8 at 1:03












  • ...actually, if you're no longer doing redirection inside the script, you don't need the { and } at all (since all they do is scope that redirection, such that stdin comes from the file within that block).
    – Charles Duffy
    Nov 8 at 1:08












  • Thanks Charles Duffy the code works. I had to comment out the line [[ $target_idx ]] || { echo "ERROR: No $target field found" >&2; exit 1; } for some reason i was getting echo not found. Now I will try to go through the code line by line and try to understand it to the best of my ability.
    – kumarm
    Nov 8 at 2:12












  • Can you reproduce that not-found error on an online interpreter (like how I have an example where you can see the code working at ideone.com)? Anyhow, let me know if you have questions that aren't covered in the references section and I'll add more links as appropriate.
    – Charles Duffy
    Nov 8 at 4:18




















up vote
0
down vote













Try this also:



idline=$(grep id input.txt)
IFS=',' read -ra elem <<< "$idline"
idfld=0
for i in "${elem[@]}"; do
idfld=$(($idfld+1))
if [[ "$i" = "id" ]]
then
break;
fi
done
gt=0
for num in `cat input.txt|grep "^[0-9]"|cut -d"," -f${idfld}`; do
gt=$(($gt+$num ))
done
echo $gt





share|improve this answer























  • Thanks but this would not work in my case as I do not know which field/position id column would be.
    – kumarm
    Nov 8 at 1:58










  • Hi this is the error i am facing when i try this warning: here-document at line 3 delimited by end-of-file (wanted `$idline') I think its because of some un printable characters or the online bash terminal I am using. i will try it out tomorrow when i go to office and revert back to you.Thanks for the time
    – kumarm
    Nov 8 at 3:31












  • Sorry for the delay @sam yes your script works it gave an error saying cut starts at position 1 so i made some changes but it works thanks.
    – kumarm
    Nov 10 at 18:10











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199880%2fhow-do-you-calculate-sum-of-a-column-without-creating-temp-files-in-shell-script%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








up vote
0
down vote



accepted










#!/usr/bin/env bash

target="id" # the field you want to sum
target_idx= # the column number of that field
sum=0 # the sum that was found so far

{
# first, just read the header...
IFS=, read -r -a header
for idx in "${!header[@]}"; do # and look for the target field in it
[[ ${header[$idx]} = $target ]] && { target_idx=$idx; break; }
done
[[ $target_idx ]] || { echo "ERROR: No $target field found" >&2; exit 1; }

# then, iterate over other lines
while IFS=, read -r -a line; do
sum=$(( sum + ${line[$target_idx]} ))
done
} <testfile.txt

echo "$sum"


See this running at https://ideone.com/MOnpFM





Some references:




  • The basics of reading a file line-by-line by calling read repeatedly are covered in BashFAQ #1.

  • Fancy array syntax (including the parameter expansion "${!array[@]}" to iterate over indices) is covered in the bash-hackers' page on arrays, BashFAQ #5, and also the BashGuide array page.

  • Arithmetic syntax ($(( ... )) for math) is covered in the bash-hackers' wiki at http://wiki.bash-hackers.org/syntax/arith_expr






share|improve this answer























  • Thank-you for your time. quick question how do I pass my input file name to the script mentioned
    – kumarm
    Nov 8 at 1:02










  • Personally, I'd just take out the <testfile.txt from inside the script and connect the input to stdin; thus, invoking it as ./yourscript <"$realInputFileName" (assuming realInputFileName is a variable that holds, well, the input file's name).
    – Charles Duffy
    Nov 8 at 1:03












  • ...actually, if you're no longer doing redirection inside the script, you don't need the { and } at all (since all they do is scope that redirection, such that stdin comes from the file within that block).
    – Charles Duffy
    Nov 8 at 1:08












  • Thanks Charles Duffy the code works. I had to comment out the line [[ $target_idx ]] || { echo "ERROR: No $target field found" >&2; exit 1; } for some reason i was getting echo not found. Now I will try to go through the code line by line and try to understand it to the best of my ability.
    – kumarm
    Nov 8 at 2:12












  • Can you reproduce that not-found error on an online interpreter (like how I have an example where you can see the code working at ideone.com)? Anyhow, let me know if you have questions that aren't covered in the references section and I'll add more links as appropriate.
    – Charles Duffy
    Nov 8 at 4:18

















up vote
0
down vote



accepted










#!/usr/bin/env bash

target="id" # the field you want to sum
target_idx= # the column number of that field
sum=0 # the sum that was found so far

{
# first, just read the header...
IFS=, read -r -a header
for idx in "${!header[@]}"; do # and look for the target field in it
[[ ${header[$idx]} = $target ]] && { target_idx=$idx; break; }
done
[[ $target_idx ]] || { echo "ERROR: No $target field found" >&2; exit 1; }

# then, iterate over other lines
while IFS=, read -r -a line; do
sum=$(( sum + ${line[$target_idx]} ))
done
} <testfile.txt

echo "$sum"


See this running at https://ideone.com/MOnpFM





Some references:




  • The basics of reading a file line-by-line by calling read repeatedly are covered in BashFAQ #1.

  • Fancy array syntax (including the parameter expansion "${!array[@]}" to iterate over indices) is covered in the bash-hackers' page on arrays, BashFAQ #5, and also the BashGuide array page.

  • Arithmetic syntax ($(( ... )) for math) is covered in the bash-hackers' wiki at http://wiki.bash-hackers.org/syntax/arith_expr






share|improve this answer























  • Thank-you for your time. quick question how do I pass my input file name to the script mentioned
    – kumarm
    Nov 8 at 1:02










  • Personally, I'd just take out the <testfile.txt from inside the script and connect the input to stdin; thus, invoking it as ./yourscript <"$realInputFileName" (assuming realInputFileName is a variable that holds, well, the input file's name).
    – Charles Duffy
    Nov 8 at 1:03












  • ...actually, if you're no longer doing redirection inside the script, you don't need the { and } at all (since all they do is scope that redirection, such that stdin comes from the file within that block).
    – Charles Duffy
    Nov 8 at 1:08












  • Thanks Charles Duffy the code works. I had to comment out the line [[ $target_idx ]] || { echo "ERROR: No $target field found" >&2; exit 1; } for some reason i was getting echo not found. Now I will try to go through the code line by line and try to understand it to the best of my ability.
    – kumarm
    Nov 8 at 2:12












  • Can you reproduce that not-found error on an online interpreter (like how I have an example where you can see the code working at ideone.com)? Anyhow, let me know if you have questions that aren't covered in the references section and I'll add more links as appropriate.
    – Charles Duffy
    Nov 8 at 4:18















up vote
0
down vote



accepted







up vote
0
down vote



accepted






#!/usr/bin/env bash

target="id" # the field you want to sum
target_idx= # the column number of that field
sum=0 # the sum that was found so far

{
# first, just read the header...
IFS=, read -r -a header
for idx in "${!header[@]}"; do # and look for the target field in it
[[ ${header[$idx]} = $target ]] && { target_idx=$idx; break; }
done
[[ $target_idx ]] || { echo "ERROR: No $target field found" >&2; exit 1; }

# then, iterate over other lines
while IFS=, read -r -a line; do
sum=$(( sum + ${line[$target_idx]} ))
done
} <testfile.txt

echo "$sum"


See this running at https://ideone.com/MOnpFM





Some references:




  • The basics of reading a file line-by-line by calling read repeatedly are covered in BashFAQ #1.

  • Fancy array syntax (including the parameter expansion "${!array[@]}" to iterate over indices) is covered in the bash-hackers' page on arrays, BashFAQ #5, and also the BashGuide array page.

  • Arithmetic syntax ($(( ... )) for math) is covered in the bash-hackers' wiki at http://wiki.bash-hackers.org/syntax/arith_expr






share|improve this answer














#!/usr/bin/env bash

target="id" # the field you want to sum
target_idx= # the column number of that field
sum=0 # the sum that was found so far

{
# first, just read the header...
IFS=, read -r -a header
for idx in "${!header[@]}"; do # and look for the target field in it
[[ ${header[$idx]} = $target ]] && { target_idx=$idx; break; }
done
[[ $target_idx ]] || { echo "ERROR: No $target field found" >&2; exit 1; }

# then, iterate over other lines
while IFS=, read -r -a line; do
sum=$(( sum + ${line[$target_idx]} ))
done
} <testfile.txt

echo "$sum"


See this running at https://ideone.com/MOnpFM





Some references:




  • The basics of reading a file line-by-line by calling read repeatedly are covered in BashFAQ #1.

  • Fancy array syntax (including the parameter expansion "${!array[@]}" to iterate over indices) is covered in the bash-hackers' page on arrays, BashFAQ #5, and also the BashGuide array page.

  • Arithmetic syntax ($(( ... )) for math) is covered in the bash-hackers' wiki at http://wiki.bash-hackers.org/syntax/arith_expr







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 1:07

























answered Nov 8 at 0:51









Charles Duffy

170k24193247




170k24193247












  • Thank-you for your time. quick question how do I pass my input file name to the script mentioned
    – kumarm
    Nov 8 at 1:02










  • Personally, I'd just take out the <testfile.txt from inside the script and connect the input to stdin; thus, invoking it as ./yourscript <"$realInputFileName" (assuming realInputFileName is a variable that holds, well, the input file's name).
    – Charles Duffy
    Nov 8 at 1:03












  • ...actually, if you're no longer doing redirection inside the script, you don't need the { and } at all (since all they do is scope that redirection, such that stdin comes from the file within that block).
    – Charles Duffy
    Nov 8 at 1:08












  • Thanks Charles Duffy the code works. I had to comment out the line [[ $target_idx ]] || { echo "ERROR: No $target field found" >&2; exit 1; } for some reason i was getting echo not found. Now I will try to go through the code line by line and try to understand it to the best of my ability.
    – kumarm
    Nov 8 at 2:12












  • Can you reproduce that not-found error on an online interpreter (like how I have an example where you can see the code working at ideone.com)? Anyhow, let me know if you have questions that aren't covered in the references section and I'll add more links as appropriate.
    – Charles Duffy
    Nov 8 at 4:18




















  • Thank-you for your time. quick question how do I pass my input file name to the script mentioned
    – kumarm
    Nov 8 at 1:02










  • Personally, I'd just take out the <testfile.txt from inside the script and connect the input to stdin; thus, invoking it as ./yourscript <"$realInputFileName" (assuming realInputFileName is a variable that holds, well, the input file's name).
    – Charles Duffy
    Nov 8 at 1:03












  • ...actually, if you're no longer doing redirection inside the script, you don't need the { and } at all (since all they do is scope that redirection, such that stdin comes from the file within that block).
    – Charles Duffy
    Nov 8 at 1:08












  • Thanks Charles Duffy the code works. I had to comment out the line [[ $target_idx ]] || { echo "ERROR: No $target field found" >&2; exit 1; } for some reason i was getting echo not found. Now I will try to go through the code line by line and try to understand it to the best of my ability.
    – kumarm
    Nov 8 at 2:12












  • Can you reproduce that not-found error on an online interpreter (like how I have an example where you can see the code working at ideone.com)? Anyhow, let me know if you have questions that aren't covered in the references section and I'll add more links as appropriate.
    – Charles Duffy
    Nov 8 at 4:18


















Thank-you for your time. quick question how do I pass my input file name to the script mentioned
– kumarm
Nov 8 at 1:02




Thank-you for your time. quick question how do I pass my input file name to the script mentioned
– kumarm
Nov 8 at 1:02












Personally, I'd just take out the <testfile.txt from inside the script and connect the input to stdin; thus, invoking it as ./yourscript <"$realInputFileName" (assuming realInputFileName is a variable that holds, well, the input file's name).
– Charles Duffy
Nov 8 at 1:03






Personally, I'd just take out the <testfile.txt from inside the script and connect the input to stdin; thus, invoking it as ./yourscript <"$realInputFileName" (assuming realInputFileName is a variable that holds, well, the input file's name).
– Charles Duffy
Nov 8 at 1:03














...actually, if you're no longer doing redirection inside the script, you don't need the { and } at all (since all they do is scope that redirection, such that stdin comes from the file within that block).
– Charles Duffy
Nov 8 at 1:08






...actually, if you're no longer doing redirection inside the script, you don't need the { and } at all (since all they do is scope that redirection, such that stdin comes from the file within that block).
– Charles Duffy
Nov 8 at 1:08














Thanks Charles Duffy the code works. I had to comment out the line [[ $target_idx ]] || { echo "ERROR: No $target field found" >&2; exit 1; } for some reason i was getting echo not found. Now I will try to go through the code line by line and try to understand it to the best of my ability.
– kumarm
Nov 8 at 2:12






Thanks Charles Duffy the code works. I had to comment out the line [[ $target_idx ]] || { echo "ERROR: No $target field found" >&2; exit 1; } for some reason i was getting echo not found. Now I will try to go through the code line by line and try to understand it to the best of my ability.
– kumarm
Nov 8 at 2:12














Can you reproduce that not-found error on an online interpreter (like how I have an example where you can see the code working at ideone.com)? Anyhow, let me know if you have questions that aren't covered in the references section and I'll add more links as appropriate.
– Charles Duffy
Nov 8 at 4:18






Can you reproduce that not-found error on an online interpreter (like how I have an example where you can see the code working at ideone.com)? Anyhow, let me know if you have questions that aren't covered in the references section and I'll add more links as appropriate.
– Charles Duffy
Nov 8 at 4:18














up vote
0
down vote













Try this also:



idline=$(grep id input.txt)
IFS=',' read -ra elem <<< "$idline"
idfld=0
for i in "${elem[@]}"; do
idfld=$(($idfld+1))
if [[ "$i" = "id" ]]
then
break;
fi
done
gt=0
for num in `cat input.txt|grep "^[0-9]"|cut -d"," -f${idfld}`; do
gt=$(($gt+$num ))
done
echo $gt





share|improve this answer























  • Thanks but this would not work in my case as I do not know which field/position id column would be.
    – kumarm
    Nov 8 at 1:58










  • Hi this is the error i am facing when i try this warning: here-document at line 3 delimited by end-of-file (wanted `$idline') I think its because of some un printable characters or the online bash terminal I am using. i will try it out tomorrow when i go to office and revert back to you.Thanks for the time
    – kumarm
    Nov 8 at 3:31












  • Sorry for the delay @sam yes your script works it gave an error saying cut starts at position 1 so i made some changes but it works thanks.
    – kumarm
    Nov 10 at 18:10















up vote
0
down vote













Try this also:



idline=$(grep id input.txt)
IFS=',' read -ra elem <<< "$idline"
idfld=0
for i in "${elem[@]}"; do
idfld=$(($idfld+1))
if [[ "$i" = "id" ]]
then
break;
fi
done
gt=0
for num in `cat input.txt|grep "^[0-9]"|cut -d"," -f${idfld}`; do
gt=$(($gt+$num ))
done
echo $gt





share|improve this answer























  • Thanks but this would not work in my case as I do not know which field/position id column would be.
    – kumarm
    Nov 8 at 1:58










  • Hi this is the error i am facing when i try this warning: here-document at line 3 delimited by end-of-file (wanted `$idline') I think its because of some un printable characters or the online bash terminal I am using. i will try it out tomorrow when i go to office and revert back to you.Thanks for the time
    – kumarm
    Nov 8 at 3:31












  • Sorry for the delay @sam yes your script works it gave an error saying cut starts at position 1 so i made some changes but it works thanks.
    – kumarm
    Nov 10 at 18:10













up vote
0
down vote










up vote
0
down vote









Try this also:



idline=$(grep id input.txt)
IFS=',' read -ra elem <<< "$idline"
idfld=0
for i in "${elem[@]}"; do
idfld=$(($idfld+1))
if [[ "$i" = "id" ]]
then
break;
fi
done
gt=0
for num in `cat input.txt|grep "^[0-9]"|cut -d"," -f${idfld}`; do
gt=$(($gt+$num ))
done
echo $gt





share|improve this answer














Try this also:



idline=$(grep id input.txt)
IFS=',' read -ra elem <<< "$idline"
idfld=0
for i in "${elem[@]}"; do
idfld=$(($idfld+1))
if [[ "$i" = "id" ]]
then
break;
fi
done
gt=0
for num in `cat input.txt|grep "^[0-9]"|cut -d"," -f${idfld}`; do
gt=$(($gt+$num ))
done
echo $gt






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 2:59

























answered Nov 8 at 1:50









Sam

31126




31126












  • Thanks but this would not work in my case as I do not know which field/position id column would be.
    – kumarm
    Nov 8 at 1:58










  • Hi this is the error i am facing when i try this warning: here-document at line 3 delimited by end-of-file (wanted `$idline') I think its because of some un printable characters or the online bash terminal I am using. i will try it out tomorrow when i go to office and revert back to you.Thanks for the time
    – kumarm
    Nov 8 at 3:31












  • Sorry for the delay @sam yes your script works it gave an error saying cut starts at position 1 so i made some changes but it works thanks.
    – kumarm
    Nov 10 at 18:10


















  • Thanks but this would not work in my case as I do not know which field/position id column would be.
    – kumarm
    Nov 8 at 1:58










  • Hi this is the error i am facing when i try this warning: here-document at line 3 delimited by end-of-file (wanted `$idline') I think its because of some un printable characters or the online bash terminal I am using. i will try it out tomorrow when i go to office and revert back to you.Thanks for the time
    – kumarm
    Nov 8 at 3:31












  • Sorry for the delay @sam yes your script works it gave an error saying cut starts at position 1 so i made some changes but it works thanks.
    – kumarm
    Nov 10 at 18:10
















Thanks but this would not work in my case as I do not know which field/position id column would be.
– kumarm
Nov 8 at 1:58




Thanks but this would not work in my case as I do not know which field/position id column would be.
– kumarm
Nov 8 at 1:58












Hi this is the error i am facing when i try this warning: here-document at line 3 delimited by end-of-file (wanted `$idline') I think its because of some un printable characters or the online bash terminal I am using. i will try it out tomorrow when i go to office and revert back to you.Thanks for the time
– kumarm
Nov 8 at 3:31






Hi this is the error i am facing when i try this warning: here-document at line 3 delimited by end-of-file (wanted `$idline') I think its because of some un printable characters or the online bash terminal I am using. i will try it out tomorrow when i go to office and revert back to you.Thanks for the time
– kumarm
Nov 8 at 3:31














Sorry for the delay @sam yes your script works it gave an error saying cut starts at position 1 so i made some changes but it works thanks.
– kumarm
Nov 10 at 18:10




Sorry for the delay @sam yes your script works it gave an error saying cut starts at position 1 so i made some changes but it works thanks.
– kumarm
Nov 10 at 18:10


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199880%2fhow-do-you-calculate-sum-of-a-column-without-creating-temp-files-in-shell-script%23new-answer', 'question_page');
}
);

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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini