• Blogging
  • Freebies
  • Freeware
  • How-To
  • Make Money Online
  • Mobile
  • Home >> Unix >> Find Min and Max of numbers from a File in Unix – Unix Commands

    Find Min and Max of numbers from a File in Unix – Unix Commands


    Finding Minimum (Min) and Maximum (Max) of numbers from database table is very easy. You can find out using min and max aggregate functions in SQL.

    But what if you have a file in Unix which contains number in each line ?

    Well there are multiple ways to find out the Min and Max of numbers from a file in UNIX. Below are few ways:

    Check out the Difference Between Unix and Linux

    For instance we have a file numbers.txt containing following values:

    4
    6
    3
    7
    2

    Find Min and Max of numbers from a file using Sort and Head Unix Command

    # sorts in descending order and take the first number using head which will give you max number.
    sort -n -r numbers.txt | head -1 
    # sorts in ascending order (default) and take the first number using head which will give you min number
    sort -n numbers.txt | head -1 

    Find Min and Max of numbers from a file using Sort and Tail Unix Command

    # sorts in ascending order (default) and take the last number using tail which will give you max number.
    sort -n numbers.txt | tail -1
    # sorts in descending order and take the last number using tail which will give you min number.
    sort -n -r numbers.txt | tail -1 

    Explanation of above sort command

    sort -n : instructs that content of each line should be treated as numeric value.
    sort -r : sort in descending order (default is ascending order).

    Hope you were able to find minimum and maximum of numbers from a file in Unix.

    If you know any other command, do share by leaving a comment. I would be happy to test it and include in this above post

    IMPORTANT: This blog welcomes Guest Bloggers, Writers to also contribute by writing Guest Posts and also Make Money!. Check out our Revenue Sharing Program
    1924 readers are already subscribed to this blog! Why don't you be one of them? Subscribe to this blog via your favorite RSS feed reader or by entering your email address on the form below:





    Leave a Reply

    page counter
    NiharsWorld on Twitter NiharsWorld on Facebook NiharsWorld RSS Feed