awk: find minimum and maximum in column

前端 未结 4 423
慢半拍i
慢半拍i 2020-12-09 17:00

I\'m using awk to deal with a simple .dat file, which contains several lines of data and each line has 4 columns separated by a single space. I want to fin

4条回答
  •  长情又很酷
    2020-12-09 18:01

    a non-awk answer:

    cut -d" " -f1 file |
    sort -n |
    tee >(echo "min=$(head -1)") \
      > >(echo "max=$(tail -1)")
    

    That tee command is perhaps a bit much too clever. tee duplicates its stdin stream to the files names as arguments, plus it streams the same data to stdout. I'm using process substitutions to filter the streams.

    The same effect can be used (with less flourish) to extract the first and last lines of a stream of data:

    cut -d" " -f1 file | sort -n | sed -n '1s/^/min=/p; $s/^/max=/p'
    

    or

    cut -d" " -f1 file | sort -n | { 
        read line
        echo "min=$line"
        while read line; do max=$line; done
        echo "max=$max"
    }
    

提交回复
热议问题