Find the answer to your Linux question:
Results 1 to 5 of 5
If I have a txt file that looks like this: James_Bill 6 Lee_Kim 6 Cain_Tommy 6 James_Bill 8 Lee_Kim 8 Cain_Tommy 8 James_Bill 12 Lee_Kim 12 Cain_Tommy 12 James_Bill 20 ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    2

    Help with sort command

    If I have a txt file that looks like this:

    James_Bill 6
    Lee_Kim 6
    Cain_Tommy 6
    James_Bill 8
    Lee_Kim 8
    Cain_Tommy 8
    James_Bill 12
    Lee_Kim 12
    Cain_Tommy 12
    James_Bill 20
    Lee_Kim 20
    Cain_Tommy 20
    James_Bill 50
    Lee_Kim 50
    Cain_Tommy 50
    James_Bill 100
    Lee_Kim 100
    James_Bill 500
    Lee_Kim 500
    Cain_Tommy 500
    James_Bill 1000
    Lee_Kim 1000
    Cain_Tommy 1000

    But I need to sort it alphabetically by the first column and then numerically by the second column so that it looks like this:

    Cain_Tommy 6
    Cain_Tommy 8
    Cain_Tommy 12
    Cain_Tommy 20
    Cain_Tommy 50
    Cain_Tommy 100
    Cain_Tommy 500
    Cain_Tommy 1000
    James_Bill 6
    James_Bill 8
    James_Bill 12
    James_Bill 20
    James_Bill 50
    James_Bill 100
    James_Bill 500
    James_Bill 1000
    Lee_Kim 6
    Lee_Kim 8
    Lee_Kim 12
    Lee_Kim 20
    Lee_Kim 50
    Lee_Kim 100
    Lee_Kim 500
    Lee_Kim 1000

    How can I use the sort command to accomplish this?

    Thanks in advance!

  2. #2
    Linux User vickey_20's Avatar
    Join Date
    Mar 2009
    Location
    Mumbai, India
    Posts
    493

    Use sort command to sort the file

    If you want the content of the file text to be sorted in another file( which you can rename afterward to text) use the sort command.
    Code:
    cat text | sort >> text1
    The above code will create a file tex1 with all content sorted.You can then delete the original file and rename the text1 to text. Hope this is what you were looking for

  3. #3
    Just Joined!
    Join Date
    May 2009
    Posts
    2
    So my problem is that when I sort the text file the numeric column looks like:

    1000
    100
    500
    12
    20
    50
    6
    8

    for each of the names. I need them in order like:

    6
    8
    12
    20
    50
    100
    500
    1000

  4. #4
    Linux User vickey_20's Avatar
    Join Date
    Mar 2009
    Location
    Mumbai, India
    Posts
    493

    check out the manual for sort

    Please check out the manual for sort command you will find something to sort data in reverse order( may be -r I'm not sure).

  5. #5
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,096
    try this:
    sort -k1d,1 -k2n,2 textfile

    It says:
    Sort the field 1 to 1 dictionary
    and sort the field 2 to 2 numerical.

Posting Permissions

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