Score:0

how can i find the average of data in a column in a txt file?

ee flag

I need to write a script that can find the average of numbers in a column and then print out the value when run. I am quite new to Linux commands, and I've tried my best, but I don't even know how to begin. I thought I need to make a counter somehow and use the awk command, but I don't know how to make it read the data in the column or to then divide over the number of entries. it might sound simple to most, but I started using Linux 2 days ago so I'm quite lost and would appreciate any help.

us flag
You can do this with LibreOffice Spreadsheet (an alternative software of Microsoft Excel). Is there any particular reason you are trying to use the command line to calculate average of numbers? Are you using any particular language? What exactly are you trying to do? Please edit the question and add more information.
hr flag
This sounds like an elementary homework exercise - there are many examples, even here on AskUbuntu ex. [create sum, avg and min of file](https://askubuntu.com/questions/640437/create-sum-avg-and-min-of-file)
cn flag
python: read lines of file, split lines, make column you need to count a float or inti, print sum and done :) Your q is a simple coding question ;-)
Gilles Quenot avatar
tn flag
Advice to newcomers: If an answer solves your problem, please accept it by clicking the large check mark (✓) next to it and optionally also up-vote it (up-voting requires at least 15 reputation points). If you found other answers helpful, please up-vote them. Accepting and up-voting helps future readers.
Score:4
tn flag

Like this:

$ cat file
1
2
3
4
5
6
$ awk '
    {count+=$1}
    END{print count/NR}
' file

Output

3.5

Explanations

Operator Meaning
{} awk operation/expression/code
count a random variable
+=$1 + value of column1 increment
END or true condition code to run... at the END or on true condition. awk is all about true{expr} like 1{print}
count/NR simple arithmetic division
NR number of the current/last line: Number of Records
BeastOfCaerbannog avatar
ca flag
There was a backtick left open and was breaking the formatting. ;)
Gilles Quenot avatar
tn flag
Reported [here](https://meta.stackexchange.com/questions/388490/missing-a-backtick-from-markdown-table-in-preview-was-well-rendered-but-not-ser)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.