0

How can I ignore the space in "Kuala Lumpur" when sorting this list? I cheated it by tabbing the columns and sorted on the tab which gives me the right results but I would like to know how to deal with a space in a column because re-formatting a list doesn't seem like a good habit to get into, especially if the list is much larger. Is that possible?

Thanks in advance

  Kuala Lumpur        78          56

  Seoul               86          66

  Karachi             95          75

  Tokyo               85          60

  Lahore              85          75

  Manila              90          85

BY CITY:

Karachi             95          75

Kuala Lumpur        78          56

Lahore              85          75

Manila              90          85

Seoul               86          66

Tokyo               85          60

I also have it sorted by high temp (high-low, 2nd column) and low temp (low-high, 3rd col)

BY HIGH TEMP:

 Karachi             95          75

 Manila              90          85

 Seoul               86          66

 Lahore              85          75

 Tokyo               85          60

 Kuala Lumpur        78          56

BY LOW TEMP:

  Kuala Lumpur        78          56

  Tokyo               85          60

  Seoul                86          66

  Karachi             95          75

  Lahore              85          75

  Manila              90          85

original without tabs

  Kuala Lumpur 78 56

  Tokyo        85 60

  Seoul        86 66

  Karachi      95 75

  Lahore       85 75

  Manila       90 85
10
  • 2
    Why would ignoring the space buy you anything? It would make a difference only if you had other entries beginning Kuala . Commented Apr 4, 2013 at 5:48
  • What if there were other entries with a space in them and part of the first column? Commented Apr 4, 2013 at 5:58
  • 1
    The space makes a difference only if you have more than 1 entry starting with the string before the space. I.e. if you had both Kuala Lumpur and, say, KualaLumbago. Otherwise it will not affect the sort order. Commented Apr 4, 2013 at 6:01
  • 1
    @healix: If you want strict column numbers to be maintained then your input file has to adhere to that. I mean you cannot have space as delimiter and space inside a column value as well. Either use some other delimiter or quote the columns (as it is done in CSV files). Commented Apr 4, 2013 at 6:19
  • 1
    Ah, I see. Either the format you posted reflects the real file, in which case you can sort based on character positions, or you didn't post the actual input. If it's the latter please edit your post to reflect the real situation. Commented Apr 4, 2013 at 6:21

1 Answer 1

1
jim@HOME7 ~
$ cat test.dat
Kuala Lumpur 78 56
Tokyo        85 60
Seoul        86 66
Karachi      95 75
Lahore       85 75
Manila       90 85

jim@HOME7 ~
$ sort -k1.1,1.13 test.dat
Karachi      95 75
Kuala Lumpur 78 56
Lahore       85 75
Manila       90 85
Seoul        86 66
Tokyo        85 60

jim@HOME7 ~
$ sort -k1.14,1.15 test.dat
Kuala Lumpur 78 56
Lahore       85 75
Tokyo        85 60
Seoul        86 66
Manila       90 85
Karachi      95 75

jim@HOME7 ~
$ sort -k1.17,1.18 test.dat
Kuala Lumpur 78 56
Tokyo        85 60
Seoul        86 66
Karachi      95 75
Lahore       85 75
Manila       90 85
Sign up to request clarification or add additional context in comments.

1 Comment

I was just reading and testing about character sorting. Thanks so much. I was starting to go insane.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.