7beggars_nnnnm
7beggars_nnnnm

Reputation: 777

awk - Print all lines containing the Max value found in the initial analysis (Containing U+2500 Unicode Character between the lines)

I have an issue that was answered here awk - Print all lines containing the Max value found in the initial analysis, but that now needs to be adapted to work when there is U+2500 unicode character between the lines.

The problem is as follows below, I have a new entry file as below:

0.0008    6
────────────
9.0    10
────────────
9.0    19
────────────
0.7    33

If I try to find the maximum value using the answers to awk - Print all lines containing the Max value found in the initial analysis, the output is always being as shown below:

──────
──────
──────

This is not the expected exit, But I should get something like:

9.0    10
9.0    19

Note: This issue was created so as not to compromise the choice of the solution marked as "resolved" in the awk - Print all lines containing the Max value found in the initial analysis.

Upvotes: 2

Views: 77

Answers (2)

anubhava
anubhava

Reputation: 786291

You may use this 2 pass awk as well:

awk '$1+0 != $1 {next} FNR==NR {if (max < $1) max=$1; next} $1 == max' file{,}

9.0    10
9.0    19

We compute max in first phase and ignore all lines where $1 is non-numeric, then in 2nd phase print all records that have $1 same as max value.

Upvotes: 2

RavinderSingh13
RavinderSingh13

Reputation: 133770

With your shown samples, please try following. Written and tested in GNU awk.

awk '
$1+0==$1{
  max=(max>$1?max:$1)
  arr[$1]=(arr[$1]?arr[$1] ORS:"")$0
}
END{
  print arr[max]
}
'  Input_file

Explanation: Adding detailed explanation for above solution.

awk '                                     ##Starting awk program from here.
$1+0==$1{                                 ##Checking condition if 1st field is an integer.
  max=(max>$1?max:$1)                     ##Create max variable by having maximum value out of 1st field and max variable here.
  arr[$1]=(arr[$1]?arr[$1] ORS:"")$0      ##Create array with index of $1 and keep adding its value to it wit same index values.
}
END{                                      ##Starting END block of this program from here.
  print arr[max]                          ##Printing array arr value with key of max here.
}
'  Input_file                             ##Mentioning Input_file name here.

NOTE: As per @karafka's suggestion adding $1+0==$1 so that scientific notation, negative numbers are not missed

Upvotes: 1

Related Questions