3

I have a file that contains some data in column format, for example:

219m
219m
3.9g
3.9g
4.0g
4.0g
4.1g
4.1g
4.2g
4.2g
4.2g

I want to compare the data and find the greatest value of these. Here g stands for GB and m stands for MB. How would I find the greatest number using a Linux shell script?

1
  • you can take a look at the units man page.
    – Reuben L.
    Commented Feb 28, 2014 at 18:18

1 Answer 1

6

sort -h can do this for you, but it needs the magnitude suffix (i.e. 'g' and 'm') to be in capitals. So for your data, you could do

tr a-z A-Z < filename | sort -h

In some locales, the . is not recognized as the decimal separator (see Decimal separator: Examples of use [Wikipedia]). If that is the case, modify the command as e.g.:

tr a-z A-Z < filename | LC_NUMERIC=C sort -h
4
  • This actually does not work in this case (at least not better than regular sort or sort -n). I guess it's the decimal separator that confuses sort -h. Commented Feb 28, 2014 at 12:16
  • 1
    Sorry, yes it was the decimal separator, but changing locale fixed that. I made a mistake when I first tested this, since I only changed LANG on the machine, where LC_NUMERIC was also set to override. It might be good information to include in the answer. tr a-z A-Z < filename | LC_NUMERIC=C sort -h fixes things if the user is in an area where . is not the native decimal separator. Commented Feb 28, 2014 at 12:48
  • Good spot -- I never would have thought of that!
    – Flup
    Commented Feb 28, 2014 at 15:05
  • Related: List of countries by decimal separator used. Being part of the first row in that table made this an easy (inevitable, even) find for me! :-D (I downvoted this answer since it didn't seem to work before I found that the problem was local, and SU won't let me change the vote unless the post is edited, so I'll do that). Commented Feb 28, 2014 at 17:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .