Skip to main content
added 11 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
$ awk 'NF > m { m = NF } { s += NF } END { printf("Max = %d\nAvg = %g\n", m, s/NR) }' data.in
Max = 9
Avg = 3.33333

The awk script will keep track of the maximum number of fields (columns) in m and the sum of the number of fields in s. AtWhen 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.in
Max = 9 (6)
Avg = 3.33333
$ awk 'NF > m { m = NF } { s += NF } END { printf("Max = %d\nAvg = %g\n", m, s/NR) }' data.in
Max = 9
Avg = 3.33333

The awk script will keep track of the maximum number of fields (columns) in m and the sum of the number of fields in s. At the end of 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.in
Max = 9 (6)
Avg = 3.33333
$ awk 'NF > m { m = NF } { s += NF } END { printf("Max = %d\nAvg = %g\n", m, s/NR) }' data.in
Max = 9
Avg = 3.33333

The 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.in
Max = 9 (6)
Avg = 3.33333
deleted 8 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
$ awk '{ m'NF => NFm >{ m ?= NF :} m;{ s += NF } END { printf("Max = %d\nAvg = %g\n", m, s/NR) }' data.in
Max = 9
Avg = 3.33333

The awk script will keep track of the maximum number of fields (columns) in m and the sum of the number of fields in s. At the end of 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 '{ if (NF'NF > m) { m = NF; r = NR } { s += NF } END { printf("Max = %d (%d)\nAvg = %g\n", m, r, s/NR) }' data.in
Max = 9 (6)
Avg = 3.33333
$ awk '{ m = NF > m ? NF : m; s += NF } END { printf("Max = %d\nAvg = %g\n", m, s/NR) }' data.in
Max = 9
Avg = 3.33333

The awk script will keep track of the maximum number of fields (columns) in m and the sum of the number of fields in s. At the end of 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 '{ if (NF > m) { m = NF; r = NR } s += NF } END { printf("Max = %d (%d)\nAvg = %g\n", m, r, s/NR) }' data.in
Max = 9 (6)
Avg = 3.33333
$ awk 'NF > m { m = NF } { s += NF } END { printf("Max = %d\nAvg = %g\n", m, s/NR) }' data.in
Max = 9
Avg = 3.33333

The awk script will keep track of the maximum number of fields (columns) in m and the sum of the number of fields in s. At the end of 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.in
Max = 9 (6)
Avg = 3.33333
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

$ awk '{ m = NF > m ? NF : m; s += NF } END { printf("Max = %d\nAvg = %g\n", m, s/NR) }' data.in
Max = 9
Avg = 3.33333

The awk script will keep track of the maximum number of fields (columns) in m and the sum of the number of fields in s. At the end of 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 '{ if (NF > m) { m = NF; r = NR } s += NF } END { printf("Max = %d (%d)\nAvg = %g\n", m, r, s/NR) }' data.in
Max = 9 (6)
Avg = 3.33333