how to add two numbers together using bash

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have the following file:



lab1:/etc/scripts# cat /tmp/tmp.PGikhA/audit.txt 
344 server1
1 server2


I want to be able to add the numbers from each row together - so in this case, I want to add 344 + 1 and end up with 345.



So far, I have the following steps figured out:



lab-1:/etc/scripts# cat /tmp/tmp.PGikhA/audit.txt |awk 'print $1' 
344
1


But I don't know how to add them together. I know that I can just use $a + $b syntax, but how do I get the 344 and 1 into separate variables to do that?



Thanks.



EDIT 1



I'm getting two values returned instead of just the one total. Can't see what I'm doing wrong:



lab-1:/etc/scripts# cat /tmp/tmp.jcbiih/audit.txt | awk 'print $1' | awk ' sum+=$1 print 
sum'
344
345
lab-1:/etc/scripts# cat /tmp/tmp.jcbiih/audit.txt | awk ' sum+=$1 print sum'
344
345






share|improve this question






















  • Is the 355 a typo?
    – Timothy Martin
    Mar 14 at 19:27










  • Wouldn’t 344+1 be 345, not 355?
    – Jeff Schaller
    Mar 14 at 19:29










  • yes. just typo. will fix
    – dot
    Mar 14 at 19:29














up vote
1
down vote

favorite












I have the following file:



lab1:/etc/scripts# cat /tmp/tmp.PGikhA/audit.txt 
344 server1
1 server2


I want to be able to add the numbers from each row together - so in this case, I want to add 344 + 1 and end up with 345.



So far, I have the following steps figured out:



lab-1:/etc/scripts# cat /tmp/tmp.PGikhA/audit.txt |awk 'print $1' 
344
1


But I don't know how to add them together. I know that I can just use $a + $b syntax, but how do I get the 344 and 1 into separate variables to do that?



Thanks.



EDIT 1



I'm getting two values returned instead of just the one total. Can't see what I'm doing wrong:



lab-1:/etc/scripts# cat /tmp/tmp.jcbiih/audit.txt | awk 'print $1' | awk ' sum+=$1 print 
sum'
344
345
lab-1:/etc/scripts# cat /tmp/tmp.jcbiih/audit.txt | awk ' sum+=$1 print sum'
344
345






share|improve this question






















  • Is the 355 a typo?
    – Timothy Martin
    Mar 14 at 19:27










  • Wouldn’t 344+1 be 345, not 355?
    – Jeff Schaller
    Mar 14 at 19:29










  • yes. just typo. will fix
    – dot
    Mar 14 at 19:29












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have the following file:



lab1:/etc/scripts# cat /tmp/tmp.PGikhA/audit.txt 
344 server1
1 server2


I want to be able to add the numbers from each row together - so in this case, I want to add 344 + 1 and end up with 345.



So far, I have the following steps figured out:



lab-1:/etc/scripts# cat /tmp/tmp.PGikhA/audit.txt |awk 'print $1' 
344
1


But I don't know how to add them together. I know that I can just use $a + $b syntax, but how do I get the 344 and 1 into separate variables to do that?



Thanks.



EDIT 1



I'm getting two values returned instead of just the one total. Can't see what I'm doing wrong:



lab-1:/etc/scripts# cat /tmp/tmp.jcbiih/audit.txt | awk 'print $1' | awk ' sum+=$1 print 
sum'
344
345
lab-1:/etc/scripts# cat /tmp/tmp.jcbiih/audit.txt | awk ' sum+=$1 print sum'
344
345






share|improve this question














I have the following file:



lab1:/etc/scripts# cat /tmp/tmp.PGikhA/audit.txt 
344 server1
1 server2


I want to be able to add the numbers from each row together - so in this case, I want to add 344 + 1 and end up with 345.



So far, I have the following steps figured out:



lab-1:/etc/scripts# cat /tmp/tmp.PGikhA/audit.txt |awk 'print $1' 
344
1


But I don't know how to add them together. I know that I can just use $a + $b syntax, but how do I get the 344 and 1 into separate variables to do that?



Thanks.



EDIT 1



I'm getting two values returned instead of just the one total. Can't see what I'm doing wrong:



lab-1:/etc/scripts# cat /tmp/tmp.jcbiih/audit.txt | awk 'print $1' | awk ' sum+=$1 print 
sum'
344
345
lab-1:/etc/scripts# cat /tmp/tmp.jcbiih/audit.txt | awk ' sum+=$1 print sum'
344
345








share|improve this question













share|improve this question




share|improve this question








edited Mar 14 at 19:30

























asked Mar 14 at 19:02









dot

2531310




2531310











  • Is the 355 a typo?
    – Timothy Martin
    Mar 14 at 19:27










  • Wouldn’t 344+1 be 345, not 355?
    – Jeff Schaller
    Mar 14 at 19:29










  • yes. just typo. will fix
    – dot
    Mar 14 at 19:29
















  • Is the 355 a typo?
    – Timothy Martin
    Mar 14 at 19:27










  • Wouldn’t 344+1 be 345, not 355?
    – Jeff Schaller
    Mar 14 at 19:29










  • yes. just typo. will fix
    – dot
    Mar 14 at 19:29















Is the 355 a typo?
– Timothy Martin
Mar 14 at 19:27




Is the 355 a typo?
– Timothy Martin
Mar 14 at 19:27












Wouldn’t 344+1 be 345, not 355?
– Jeff Schaller
Mar 14 at 19:29




Wouldn’t 344+1 be 345, not 355?
– Jeff Schaller
Mar 14 at 19:29












yes. just typo. will fix
– dot
Mar 14 at 19:29




yes. just typo. will fix
– dot
Mar 14 at 19:29










1 Answer
1






active

oldest

votes

















up vote
5
down vote



accepted










You can do your math in awk easily. Here is an example:



awk ' total+=$1 END print total '


If you really wanted to use bash, you could use a simple loop to read one line at a time and add it up:



count=0 
while read -r number _; do # put the first column in "number" and discard the rest of the line
count=$(( count + number ))
done < /tmp/foo
echo $count





share|improve this answer




















  • when i try it via command line, I'm getting both 344 and 345. Please see edit 1
    – dot
    Mar 14 at 19:19






  • 2




    @dot You left off the "END", which tells awk to not print until it has finished processing all the data.
    – jordanm
    Mar 14 at 19:29










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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430241%2fhow-to-add-two-numbers-together-using-bash%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
5
down vote



accepted










You can do your math in awk easily. Here is an example:



awk ' total+=$1 END print total '


If you really wanted to use bash, you could use a simple loop to read one line at a time and add it up:



count=0 
while read -r number _; do # put the first column in "number" and discard the rest of the line
count=$(( count + number ))
done < /tmp/foo
echo $count





share|improve this answer




















  • when i try it via command line, I'm getting both 344 and 345. Please see edit 1
    – dot
    Mar 14 at 19:19






  • 2




    @dot You left off the "END", which tells awk to not print until it has finished processing all the data.
    – jordanm
    Mar 14 at 19:29














up vote
5
down vote



accepted










You can do your math in awk easily. Here is an example:



awk ' total+=$1 END print total '


If you really wanted to use bash, you could use a simple loop to read one line at a time and add it up:



count=0 
while read -r number _; do # put the first column in "number" and discard the rest of the line
count=$(( count + number ))
done < /tmp/foo
echo $count





share|improve this answer




















  • when i try it via command line, I'm getting both 344 and 345. Please see edit 1
    – dot
    Mar 14 at 19:19






  • 2




    @dot You left off the "END", which tells awk to not print until it has finished processing all the data.
    – jordanm
    Mar 14 at 19:29












up vote
5
down vote



accepted







up vote
5
down vote



accepted






You can do your math in awk easily. Here is an example:



awk ' total+=$1 END print total '


If you really wanted to use bash, you could use a simple loop to read one line at a time and add it up:



count=0 
while read -r number _; do # put the first column in "number" and discard the rest of the line
count=$(( count + number ))
done < /tmp/foo
echo $count





share|improve this answer












You can do your math in awk easily. Here is an example:



awk ' total+=$1 END print total '


If you really wanted to use bash, you could use a simple loop to read one line at a time and add it up:



count=0 
while read -r number _; do # put the first column in "number" and discard the rest of the line
count=$(( count + number ))
done < /tmp/foo
echo $count






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 14 at 19:06









jordanm

29k27790




29k27790











  • when i try it via command line, I'm getting both 344 and 345. Please see edit 1
    – dot
    Mar 14 at 19:19






  • 2




    @dot You left off the "END", which tells awk to not print until it has finished processing all the data.
    – jordanm
    Mar 14 at 19:29
















  • when i try it via command line, I'm getting both 344 and 345. Please see edit 1
    – dot
    Mar 14 at 19:19






  • 2




    @dot You left off the "END", which tells awk to not print until it has finished processing all the data.
    – jordanm
    Mar 14 at 19:29















when i try it via command line, I'm getting both 344 and 345. Please see edit 1
– dot
Mar 14 at 19:19




when i try it via command line, I'm getting both 344 and 345. Please see edit 1
– dot
Mar 14 at 19:19




2




2




@dot You left off the "END", which tells awk to not print until it has finished processing all the data.
– jordanm
Mar 14 at 19:29




@dot You left off the "END", which tells awk to not print until it has finished processing all the data.
– jordanm
Mar 14 at 19:29












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430241%2fhow-to-add-two-numbers-together-using-bash%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?