use the linux command to find the max value and print

Recently there was a task that processed the log file and found the row where the maximum value is.

Log file format:

123#192.168.1.33#google#20190622#/url/test
2313#192.168.2.33#baidu#20190922#/url/test1
2344#192.168.1.33#google#20190622#/url/test3
18#192.168.1.33#google#20190622#/url/test
3#192.168.1.33#google#20190622#/url/test1

First way: awk -v

➜ awk -F'#' -v max=0 '{if($1>max){content=$0; max=$1}}END{print content}' test-1.txt

Second way: awk + sort + head

➜ awk -F"#" '{print $1, $0}' test-1.txt| sort -nr | head -1

Third way: sed + sort + head

➜ sed 's/#/ /g' test-1.txt | sort -nr | head -1

Fourth way: sort -t + head

➜ sort -t "#" -k 1 -nr test-1.txt | head -1

Add a Comment

Your email address will not be published. Required fields are marked *