-1

I'm trying to select lines that have F starting them from my .txt file, and then find the average of the numbers, here's my code, I don't know why it's not adding up.

#!/bin/bash
function F1()
{
count=1;
total=0;
file='users.txt'
while read line; do
  if (grep \^"F");
   then
     for i in $( awk '{ print $3; }')
       do 
          total=$(echo $total+$i  )
          var=$((var+1))
       done
  fi
       echo "scale=2; $total / $count"  
       echo $line
  done < $file
}

F1

my output

jwvh
  • 50,871
  • 7
  • 38
  • 64
mia
  • 3
  • 1
  • 3
  • Does this answer your question? [Why does my variable set in a do loop disappear? (unix shell)](https://stackoverflow.com/questions/38799636/why-does-my-variable-set-in-a-do-loop-disappear-unix-shell) – user1934428 Feb 12 '21 at 09:48
  • Since you have referenced awk in your solution, you could probably use awk on it's own to achieve what you need. – Raman Sailopal Feb 12 '21 at 09:56
  • @RamanSailopal i'm new to it, not really sure how to go about that, could you help? – mia Feb 12 '21 at 10:34
  • Can you post an extract of the extract file as well as how you would like the output to look – Raman Sailopal Feb 12 '21 at 10:59
  • Please don't post pictures of text; see https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors – tripleee Feb 12 '21 at 12:01

2 Answers2

0

You don't perform an addition for the variable total. On the first iteration, the line

total=$(echo $total+$i  )

(assuming that i, for instance, is 4711) is expanded to

total=0+4711

So the variable total is set to a 6-character string, not a number. To actually add here, you would have to write

((total = total + i))
user1934428
  • 19,864
  • 7
  • 42
  • 87
  • i still have the same output, do you think that was the only issue? – mia Feb 12 '21 at 10:26
  • There are plenty of oddities in the script, so I picked just the most obvious one. I suggest that you run your program with `set -x`. You likely will find from this all the errors. If the effect of one statement is still unclear afterwards, ask again for that specific statement. – user1934428 Feb 12 '21 at 12:40
0

If you are using Awk anyway, use its capabilities.

awk '/^F/ { sum+=$3; count++ } END { print (count ? sum/count : 0) }' users.txt

This is a standard Awk idiom which will typically be an exercise within the first hour of any slow-paced basic beginner Awk tutorial, or within ten minutes of more directed learning.

Your shell script had at least the following errors;

  • grep without an argument will read in the rest of your input lines in one go. Running it in a subshell (i.e. inside parentheses) does nothing useful, and costs you a process.
  • The shell does not perform any arithmetic unless you separately request it; variables are simply strings. If you want to add two numbers, you need to explicitly use an arithmetic evaluation like ((total+=i)) ... like you already did in another place.
  • read without any options will mangle backlashes in your input; if you don't specifically require this behavior, you should always use read -r.
  • All except one of those semicolons are useless.
  • You should generally quote all your variables; see When to wrap quotes around a shell variable
  • If you are using Awk anyway, you should probably use it for as much as possible. The shell is very slow and inefficient when it comes to processing a file line by line, and horribly clunky when it comes to integer arithmetic (let alone then dividing numbers which are not even multiples of each other).

This is probably not complete; also try http://shellcheck.net/ before asking for human assistance.

On Stack Overflow, try to reduce your problem so that it really only asks a single question. There are duplicate questions for all of these issues, but I'll just mark this as a duplicate of the one you are actually asking about.

tripleee
  • 175,061
  • 34
  • 275
  • 318