Find the answer to your Linux question:
Results 1 to 5 of 5
I am using bash and need to sort a bunch of numbers that look like the following example: Code: 9.1 9.2 ... 9.N 10.1 10.2 ... 10.N I am having ...
  1. #1
    Linux Newbie humbletech99's Avatar
    Join Date
    Nov 2005
    Posts
    225

    Bash: sorting by two fields

    I am using bash and need to sort a bunch of numbers that look like the following example:
    Code:
    9.1
    9.2
    ...
    9.N
    10.1
    10.2
    ...
    10.N
    I am having trouble because the sort command either sorts by one field or the other, so if I just | sort -n I end up with
    Code:
    9.1
    9.20
    9.21
    ...
    9.2
    9.30
    9.31
    ...
    9.3
    ...
    10.1
    10.20
    10.21
    ...
    10.2
    Does anyone have a cunning way of sorting by field one and then subsorting by field 2 without losing the order of the first field? If I sort by field 2 then of course the 9 and 10 at the beginning would could out of order.

  2. #2
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    I don't understand the problem. What is your desired output?

  3. #3
    Linux Newbie humbletech99's Avatar
    Join Date
    Nov 2005
    Posts
    225
    I wanted to make .5 come before .40 because I am sorting IP addresses.

    It is ok, there is a little used switch -k which did what I wanted.

  4. #4
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    I think the search key switch is one of the most used, besides -n. At least, in my Linux scripts it is. Problem is, -k isn't portable. Well, it's only a problem if you intend on running the script on anything besides Linux that isn't using GNU sort.

  5. #5
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Actually, scratch that, I think it's the modifiers that may behave differently, that are sometimes used in conjunction with -k.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...