且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Shell 脚本:在不排序的整数序列中查找最大值

更新时间:2023-02-26 18:43:50

如果没有负数,你可以使用这个:

You can use this if no negative number is expected:

awk '$0>x{x=$0};END{print x}' input.txt

用它来支持负数:

awk 'BEGIN{x=-2147483648};$0>x{x=$0};END{print x}' input.txt

初始化 x 允许该解决方案正确处理值

Initializing x allows the solution to properly handle integer lists with values <= 0. See the comments for more details.