简体   繁体   English

如何比较复杂的Linux文本文件中的数字?

[英]How to compare numbers in a complex linux text file?

I have a huge file that looks like this: 我有一个看起来像这样的大文件:

1234 4567 3566 1234 4567 3566

2432 434 95054 2432 434 95054

382 9983 90 382 9983 90

with tabs separating the numbers. 用制表符分隔数字。

I'm new to linux and to BASH and I need to use the terminal (not a script in a other file) to compare all the numbers in the 3rd column. 我是Linux和BASH的新手,我需要使用终端(而不是其他文件中的脚本)来比较第三列中的所有数字。

I tried the grep command but I can't find a way to do everything I want (find the file, open it, go to the 3rd column, find the biggest number). 我尝试了grep命令,但是找不到找到所需的所有方法(查找文件,将其打开,转到第3列,找到最大的数字)。 Is it possible to do with only one command? 是否可以仅使用一条命令? With pipes maybe? 也许用管道?

Thanks for your help! 谢谢你的帮助!

It sounds like you want to print the line in your file which has the highest value in the third column. 听起来您想打印文件中第三行中具有最高值的行。 This awk script does that: 这个awk脚本可以做到:

awk '$3>max{max=$3;line=$0}END{print line}' file

$3 refers to the third column. $3引用第三列。 If it is greater than the current value of max , update max and set line to the contents of the whole line, $0 . 如果它大于max的当前值,则更新max并将line设置为整行内容$0 Once the file has been processed, print out the line with the maximum value. 处理完文件后,打印出具有最大值的行。

If you only want to find the maximum number and aren't concerned about the rest of the line, the script is slightly simpler: 如果您只想查找最大数目,而不关心其余的行,则脚本会稍微简单一些:

awk '$3>max{max=$3}END{print max}' file

This simply updates the value of max whenever the value of the third column is higher and prints it once the file has been processed. 只要第三列的值更高,这就会简单地更新max的值,并在处理完文件后立即打印它。

Try this : 尝试这个 :

$ awk '$3>v{v=$3}END{print v}' file
95054

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM