Calculate average and write it in other file

GarciSan

I have a list of students with ID and marks, and I need to make another one with their average marks. main_list:

#name surname student_index_number course_group_id lecturer_id list_of_marks

athos musketeer 1 1 1 3,4,5,3.5
porthos musketeer 2 1 1 2,5,3.5
aramis musketeer 3 2 2 2,1,4,5


And I have this script

awk '{ n = split($6, a, ","); total=0; for (v in a) total += a[v]; print total / n }' main_list

But I don't want to print it, I want to write it in other file called average marks. Final content should be like this, average_list:

athos musketeer 1 1 1 3.875
porthos musketeer 2 1 1 3.5
aramis musketeer 3 2 2 3
RavinderSingh13

Could you please try following once.

while read first second third fourth fifth sixth
do
  if [[ "$first" =~ (^#) ]]
  then
      continue
  fi
  count="${sixth//[^,]}"    
  val=$(echo "(${#count}+1)" |  bc)
  new_val=$(echo "scale=2; (${sixth//,/+})/$val" | bc)
  echo "$first $second $third $fourth $fifth $new_val"
done < "Input_file"  > "Output_file"

With your attempt try following.

awk  '{ n = split($6, a, ","); total=0; for (v in a) total += a[v]; print $1,$2,$3,$4,$5,total / n }' Input_file  > "Output_file"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Calculate average of every n rows from a csv file

Write a method for calculate average temp

Python - Calculate average for every column in a csv file

Calculate average of each column in a file

Calculate average of month and replace values of other column

read txt file and computing each lines average and write it on a new file

Read a log file and write other with the specified format

How to calculate the average score of class members in a text file?

Calculate average sales using criteria from one dataframe and aggregating in other data frame

Calculate average RGB value of a PPM file

Pick the values from specific lines of a log file and calculate an average

Cloud Composer Write file to other bucket Issues

write a function to groupby year and calculate the average and count the size in pandas

Groupby Year and other column and calculate average based on specific condition pandas

How to calculate Average rating for each movie in R from CSV File?

how to calculate average number of each person in a text file

How do I display and calculate average ? And how do I write the value to the text file only once?

Write average weather data from a year given by user to a text file

How to write method to calculate average in Swift-playground

Is it possible to write to other file descriptors in C?

Powershell - Trying to calculate the average of a csv file using a function

Calculate the average of other averages in MySQL

Calculate Average from a file and show records of students whose average is more than 90

Write way to calculate weighted average with pandas Dataframe with mean or directly multiply

Write a loop code to calculate average 77 different times, using another column as criteria

Python Pandas How to calculate the average of every other row in a column

Grouping columns of dataframe by other dataframe and calculate weighted average of aggregated columns

Calculate sum and average treating blank values with specific values based on other column condition without adding helper column

Python : Calculate average of all items in a txt.file