Sopalajo de Arrierez
Sopalajo de Arrierez

Reputation: 3860

How can I get the maximum value of specific fields in a CSV text file?

Each line in my text file example (CSV, comma separated) is like:

2016-01-10,23:56:07,10,71,47

As can be seen, fields 3, 4 and 5 are numeric values.

For each line, I would like to get only the maximum value of fields 3 and 4. Something like:

awk -F ',' '{print max($3,$4)}'

(probably not a real AWK syntax, this one, but it is the closest I can think about).
So, for the above line, the resulting number would be '71' .

Example with more lines:

2016-01-10,23:53:07,6,99,41
2016-01-10,23:54:07,10,88,44
2016-01-10,23:55:07,31,71,46
2016-01-10,23:56:07,71,10,47

The results:

99
88
71
71

How can this be done on Linux shell?
I have suggested AWK, but any other solutions are OK.

Upvotes: 3

Views: 1941

Answers (3)

dawg
dawg

Reputation: 103864

If you need more control over formatting or have quoted cvs fields, Python is a good choice:

$ python -c "
> import csv, fileinput
> for line in csv.reader(fileinput.input()): 
>    print max(map(float, line[2:4]))
> " /tmp/file.csv
99.0
88.0
71.0
71.0

Upvotes: 0

Benjamin W.
Benjamin W.

Reputation: 52152

Just Bash:

while IFS=, read -r _ _ f3 f4 _; do echo "$(( f3 > f4 ? f3 : f4 ))"; done < infile

If field 3 and 4 can contain non-integers, this can be extended using bc to support floats (making it even more obvious how the awk solution is preferrable):

while IFS=, read -r _ _ f3 f4 _; do
    bc -l <<< "if ($f3 > $f4) $f3 else $f4"
done < infile

else is a GNU extension and not supported by POSIX bc. Use if ($f3 > $f4) $f3; if ($f4 >= $f3) $f4 for POSIX conformance.

Upvotes: 2

Kent
Kent

Reputation: 195079

this one-liner may help:

awk -F, '{print ($3>$4?$3:$4)}' file

Upvotes: 7

Related Questions