how to add two numbers together using bash
Clash 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
bash shell-script
add a comment |Â
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
bash shell-script
Is the355
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
add a comment |Â
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
bash shell-script
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
bash shell-script
edited Mar 14 at 19:30
asked Mar 14 at 19:02
dot
2531310
2531310
Is the355
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
add a comment |Â
Is the355
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
add a comment |Â
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
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
add a comment |Â
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
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
add a comment |Â
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
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
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
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
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
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
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
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
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