简体   繁体   中英

sort in bash #2

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
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM