I have a file looks like:12 4 5 6 7 19202224 26 27 29 30 31 32 34 40 50 56 58234 235 270 5001234 1235 1236 12372300considering that my real data file is huge. So I want to check what is the maximum number in this data file. Also I want to check on average how many columns exists within rows. as an example in this small example, the maximum number of column is 9 (in 5th row) and on average there is 3.33 column within rows.Any Suggestion please ?
How to calculate the average number of columns across the rows as well as the maximum numbers of columns in a file in unix?
linux;text processing;awk
$ awk 'NF > m { m = NF } { s += NF } END { printf(Max = %d\nAvg = %g\n, m, s/NR) }' data.inMax = 9Avg = 3.33333The awk script will keep track of the maximum number of fields (columns) in m and the sum of the number of fields in s. When reaching the end of the input stream, it will output the stats collected.The number of fields in the current record (line) is NF, and the number of records read so far is NR.The following version will also keep track of the record with the most number of fields:awk 'NF > m { m = NF; r = NR } { s += NF } END { printf(Max = %d (%d)\nAvg = %g\n, m, r, s/NR) }' data.inMax = 9 (6)Avg = 3.33333