如何获得CSV文本文件中特定字段的最大值? [英] How can I get the maximum value of specific fields in a CSV text file?

查看:49
本文介绍了如何获得CSV文本文件中特定字段的最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文本文件示例中的每一行(CSV,逗号分隔)如下:

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

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

可以看出,字段3、4和5是数字值.

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

对于每行,我只想获取字段 3和4 maximum 值.像这样:

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)}'

(这可能不是真正的AWK语法,但这是我能想到的最接近的语法).
因此,对于上面的行,结果数字将为'71'.

(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' .

包含更多行的示例:

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

结果:

99
88
71
71

如何在 Linux shell 上完成此操作?
我建议使用AWK,但其他任何解决方案都可以.

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

推荐答案

这种一线可能会有所帮助:

this one-liner may help:

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

这篇关于如何获得CSV文本文件中特定字段的最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆