I'm trying to get the sum, 36962, to echo with $TOTAL at the end of the script, but I cannot get it to work. Any advice? This is for a school project and I am pretty rusty with Bash.

while read p; do
    awk '{ if ($1 > 100 ) print TOTAL+=$1}'
    echo $TOTAL
done < ipuniq.txt
  • 6578
  • 6723
  • 32735
  • 36962
  • ECHOS -> 0
  • bash and awk are 2 completely separate, independent tools and you wouldn't write a loop using bash to call awk one line at a time, you'd just do it all in awk (see why-is-using-a-shell-loop-to-process-text-considered-bad-practice). Post some concise, testable sample input and expected output if you'd like help. – Ed Morton Nov 16 '19 at 19:25
  • 1
    Thank you for educating me on the differences.between awk and bash. Had no clue about that. – Joshua Trimm Nov 17 '19 at 22:31
  • You can do this with awk only. awk '$1 > 100 { sum+=$1 } END { print sum }' Awk is perfectly suited for these tasks – Valentin Bajrami Nov 17 '19 at 22:33
  • 1
    Please don't post pictures of text. People answering the question will almost always use that input to actually test their answer and since you are asking people for a favor it's more polite to allow them to copy and paste rather than have to retype from an image. You can select a block of text and press ctrl + k to make a code block or surround it with ``` – jesse_b Nov 17 '19 at 22:49

The TOTAL variable is a shell variable and therefore awk won't know about it. In your awk command the TOTAL variable is unset in each iteration so it's being reset with a single value each time and then printed.

Additionally, it seems pointless to use awk in a loop like this.

awk can perform this operation by itself with the following 1 liner:

awk '$1 > 100 { total+=$1 } END { print total }' ipuniq.txt

No loops or other commands needed.

However, if your assignment requires you use bash/shell you could do:

while read -r p _; do
    ((p>100)) && total=$((total+p))
done < ipuniq.txt
echo "$total"

This will use a shell arithmetic expression to check if the value of p is greater than 100, if so it will add it to total.

Additionally note: I have added the -r option to your read command as this will print backslashes literally (likely unnecessary for your use case but I prefer to use -r at all times unless I specifically have a reason not to.)

I have also added the _ parameter name, this will prevent p from being set to an undesirable input. read will assign all leftover words to the last parameter, so in your case say your file contains numbers and IPs like:


Without a parameter to catch the cruft, p would be set to:



  • 1
    I put the echo "$total" outside of the loop and it worked great! Thank you so much for the help! – Joshua Trimm Nov 17 '19 at 22:29
  • @JoshuaTrimm: ah, I wasn't sure what your intent was and thought you wanted to print the current total on each iteration. I have updated my answer to reflect. – jesse_b Nov 17 '19 at 22:41
  • @JoshuaTrimm wrt I put the echo "$total" outside of the loop and it worked great! - you shouldn't be doing that at all, you should be using the awk-only script. – Ed Morton Nov 17 '19 at 23:23
  • How do I get the IP address with the loop? I know how to get the number and add the total. But I need to the ip address with another list of ips.... – Joshua Trimm Nov 18 '19 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.