Ali Beyit
Ali Beyit

Reputation: 414

Linux Bash Script for calculating average of multiple files

I am writing a scipt which will take parameter of the folder which it will do the job. The aim is to calculate average number of reviews and print the result next to the name of the file. I wrote the script for only one file it works okay but I couldn't find any solutions to do it on multiple files. I should get an output like ;

    % ./averagereviews.sh path_to_folder
    hotel_11212 3.51
    hotel_2121 2.62
    hotel_31212 2.43

...

I done this task for only one hotel and the code is like this;

    grep "<Overall>" $1 | sed 's/<Overall>//g'| awk '{SUM += $1} END {print SUM/NR}'

This simply searches the word "" in the file and gets the number next to it, then adds these numbers and divides the sum with NR to find average.

when I run it the output is the average value for the given hotel

    ./averagereviews.sh  hotel_190158.dat
    4.00578

But I should do this to multiple .dat files in a folder with printing the name of hotel. How can I do that ?

Upvotes: 0

Views: 726

Answers (1)

fzd
fzd

Reputation: 845

You could "cheat"

 > cat averagereviews.sh
 #!/bin/bash
 SUM=0
 data_files=$(ls $1/dataFile*.dat)
 cat $data_files | grep "<Overall>" | sed -e 's/<Overall>//g' | awk '{SUM += $1} END {print SUM/NR}'

and run (from wherever, with whichever paths you need)

> ~/tools/averagereviews.sh /tmp/data/

Simply, I'm cating all the files first, and apply your command to the rest - having it behave like the pipe is a single file.

Upvotes: 1

Related Questions