Professional Web Applications Themes

** Sort Question ** - Linux / Unix Administration

I have a question I hope some of you could shed some light on. I was trying to figure out the proper options to the sort command to do a multiple key sort on a file of email addresses. The file is basically just a list of primary and alternate email addresses separated by whitespace (tab or multiple spaces). Here is an example file. com com com com com com com com com com Actually I'm only interesting in sorting on the primary addresses but the first sort key would be the domain and the second key would be the ...

  1. #1

    Default ** Sort Question **

    I have a question I hope some of you could shed some light on.
    I was trying to figure out the proper options to the sort command
    to do a multiple key sort on a file of email addresses. The file is
    basically just a list of primary and alternate email addresses
    separated by whitespace (tab or multiple spaces). Here is an example
    file.

    com com
    com com
    com com
    com com
    com com

    Actually I'm only interesting in sorting on the primary addresses but
    the first sort key would be the domain and the second key would be the
    user.

    I'm having problems with key 1. Key 1 should begin at the first
    symbol and extend to the first whitespace, but I think the problem is
    that it is looking at everything to the second occurrence of the
    symbol. That is what I wasn't sure how to fix. I guess my question is
    can or how do you tell the key to begin at the delimiter and end at the
    first whitespace character? If possible, I would like to avoid using
    the older origin-zero syntax
    but if I must, that is fine.

    This is using version 4.5.3 of the sort from the FSF on Red Hat 9
    running bash 2.05b.

    cheers,
    validus

    validus1@gmail.com Guest

  2. #2

    Default Re: ** Sort Question **

    On Thu, 21 Apr 2005 18:48:08 -0700, validus1 wrote:
     

    Unfortunately sort only allows a single character to be specified as the
    field separator, with some special case code for '\0' and nothing
    specified. So you have to adapt your data to sort.

    There are 2 approaches
    1) Change your data so it only has a single key,
    The most obvious transformation is to change the spaces/tabs into another
    '' character.


    tr ' \t' '' < data | sort -k2,2 -k1,1 | sed 's// /2'

    You can do more fancy stuff to get the output into neater columns if you want.

    2) Add extra data, so it has the information in the manner you want, sort,
    and then remove the extra data.

    sed 's/^\(^*\)\([^ ]*\)/\2 \1&/' data | sort | sed 's/[^]*//'
    .....................^^^^^ a space and a tab character

    Icarus
    Icarus Guest

  3. #3

    Default Re: ** Sort Question **

    On Fri, 22 Apr 2005 at 01:48 GMT, com wrote: 

    TAB=$'\t' ## Use a literal tab with shell that don't support this.
    awk -F '[ $TAB]' '{printf "%s\t%s\n", $2, $0}' FILE | sort | cut -f2-
     

    --
    Chris F.A. Johnson http://cfaj.freeshell.org/shell
    ================================================== =================
    My code (if any) in this post is copyright 2005, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
    Chris Guest

  4. #4

    Default Re: ** Sort Question **

    Icarus Sparry wrote: 
    >
    > There are 2 approaches
    > 1) Change your data so it only has a single key,
    > The most obvious transformation is to change the spaces/tabs into another
    > '' character.
    >
    > tr ' \t' '' < data | sort -k2,2 -k1,1 | sed 's// /2'[/ref]

    Or you can change the at-sign into a space, as in
    sed -e '//s// /' data | sort -b -i -k2,2 -k1,1 | sed -e '/ /s///'
    ('data' would be the name of the input file)

    Kind regards,


    Daniel von Asmuth

    Daniel Guest

Similar Threads

  1. DB2 UDB 8.1 Grouping Question, sort of...
    By Brian Bouchard in forum IBM DB2
    Replies: 1
    Last Post: September 18th, 03:51 AM
  2. Replies: 1
    Last Post: September 9th, 07:16 AM
  3. sort question
    By Gunnar Hjalmarsson in forum PERL Miscellaneous
    Replies: 4
    Last Post: August 10th, 05:32 PM
  4. Sort Array Question...
    By Andrew Durstewitz in forum ASP
    Replies: 3
    Last Post: July 16th, 07:49 PM
  5. Sort of an iPhoto question
    By Fred G in forum Mac Applications & Software
    Replies: 3
    Last Post: July 2nd, 04:37 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139