How to get the biggest number in a file?

前端 未结 4 517
盖世英雄少女心
盖世英雄少女心 2020-12-04 00:41

I want to get the maximum number in a file, where numbers are integers that can occur in any place of the file.

I thought about doing the following:

         


        
4条回答
  •  清歌不尽
    2020-12-04 01:27

    I'm sure a C implementation optimized using assembler will be the fastest. Also I could think of a program which separates the file into multiple chunks and maps every chunk onto a single processor core, and afterwards just get's the maximum of nproc remaning numbers.

    Just using the existing command line tools, have you tried awk?

    time awk '{for(i=1;i<=NF;i++){m=(m<$i)?$i:m}}END{print m}' RS='$' FPAT='-{0,1}[0-9]+' myfile
    

    Looks like it can do the job in ~50% of the time compared to the perl command in the accepted answer:

    time perl -MList::Util=max -0777 -nE 'say max /-?\d+/g' myfile
    cp myfile myfile2
    
    time awk '{for(i=1;i<=NF;i++){m=(m<$i)?$i:m}}END{print m}' RS='$' FPAT='-{0,1}[0-9]+' myfile2
    

    Gave me:

    42342234
    
    real    0m0.360s
    user    0m0.340s
    sys 0m0.020s
    42342234
    
    real    0m0.193s   <-- Good job awk! You are the winner.
    user    0m0.185s
    sys 0m0.008s
    

提交回复
热议问题