0

I'm trying the following.

I have a tab-separated file with 6 columns. I want to create a new column and print there the highest value found per row across the 6 columns.

My file looks like this:

0       0.5     0.5     0       0       0
0       0       0.666667        0       0       0.333333
0.666667        0       0       0       0       0.333333
0.75    0       0       0       0       0.25
0.857143        0       0       0.142857        0       0
0.285714        0       0       0.714286        0       0
0       0.8     0       0.2     0       0
0       0.888889        0.111111        0       0       0
0       0.6     0.4     0       0       0
0       0       0.25    0.75    0       0

I would like to get this:

0       0.5     0.5     0       0       0   0.5
0       0       0.666667        0       0       0.333333    0.666667
0.666667        0       0       0       0       0.333333    0.666667
0.75    0       0       0       0       0.25    0.75
0.857143        0       0       0.142857        0       0   0.857143
0.285714        0       0       0.714286        0       0   0.714286
0       0.8     0       0.2     0       0   0.8
0       0.888889        0.111111        0       0       0   0.888889
0       0.6     0.4     0       0       0   0.6
0       0       0.25    0.75    0       0   0.75

I know how to create a new column to append my request, via

awk -F'\t' -v OFS='\t' '{ $(NF+1)=???????? ; print}'

but can't figure out how to ask awk to find the highest value between col1-col6. It should be done for each row.

2
  • 1
    How can there not be a maximum? In your example, in the first row, the maximum is 0.5, it is just present in more than one row. So we can still just look for the maximum, right? We don't also need to look for the one number that is present more than once?
    – terdon
    Commented Sep 29, 2022 at 13:41
  • Correct. I guess that's a better way to ask for it. Get the maximum value amongst columns in a new column, even when the maximum is repeated in more than one column. Thanks.
    – Claudius
    Commented Sep 29, 2022 at 14:02

1 Answer 1

3

It should be fairly easy: You can simply iterate over all fields and check which is the highest in this line. Then, append that highest value as (NF+1)th field.

awk 'BEGIN{FS=OFS="\t"} {max=$1; for (i=2;i<=NF;i++) {if (max<$i) {max=$i}}; $(NF+1)=max} 1' input.txt

Note that for a reasonable result, you should ensure that all lines have the same number of columns; the above program is flexible enough that it will parse files with different column counts per line without choking.

Update:

As noted in the answer by Ed Morton, the processing will be more efficient if you change it to

awk 'BEGIN{FS=OFS="\t"} {max=$1; for (i=2;i<=NF;i++) {if (max<$i) {max=$i}}; print $0,max}' input.txt

because this avoids the re-building of the line which awk does if you modifiy any single field (including assigning a new one).

1
  • 1
    This is the one job that OFS exists to do, just let it do it. There's no benefit in not setting OFS.
    – Ed Morton
    Commented Sep 29, 2022 at 16:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .