1

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.

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
7
  • if possible , also post desire output Apr 4, 2013 at 5:05
  • @RahulPatil updated code
    – healix
    Apr 4, 2013 at 5:10
  • unix.stackexchange.com/questions/70796/… ... does that help?
    – tink
    Apr 4, 2013 at 5:30
  • @tink No, because that's changing the format which I did and it works fine but what if I couldn't change format and had to consider the space in "Kuala Lumpur"?
    – healix
    Apr 4, 2013 at 5:32
  • is this something that can't be done?
    – healix
    Apr 4, 2013 at 5:40

3 Answers 3

0

1) convert your spaces to tab by unexpand command (consider every 8 spaces is a tab)

2) use sort with TAB delimiter in this way:

TAB=`echo -e "\t"`
sort -t"$TAB" YOUR_FILE
1
  • I've already done that. That was my work-around. Isn't there a way to ignore the space in "Kuala Lumpur" so "Lumpur" isn't seen as the 2nd column?
    – healix
    Apr 4, 2013 at 5:31
0

Note:- You question is changed twice , but I can't change answer again and again , but wish to inform you that answer will remain same, you just have to sort column number using -k2 where 2 is column , then you will get correct answer .

Just did using sort + sed

BY CITY

sort -k1 input_data | sed '/^$/d; s/$/\n/'

BY HIGH TEMP:

sort -k2 -nr data | sed '/^$/d; s/$/\n/'

BY LOW TEMP:

sort -k4 -nr data | sed '/^$/d; s/$/\n/'

Explanation:

sorted data will send to sed

then sed will delete first blank line using /^$/d then append new line after every line.

6
  • That works for the city and fisrt set of temps but when sorting second set you get: 75, 75, 85, 66, 60, 56
    – healix
    Apr 4, 2013 at 5:36
  • my question hasn't changed once. Its been "How to ignore the space in "Kuala Lumpur" field so "Lumpur" isn't seen as a column.
    – healix
    Apr 4, 2013 at 5:37
  • Please read my ans again , I have updated Apr 4, 2013 at 5:49
  • @healix also read man sort , for numeric sort use -n option to sort Apr 4, 2013 at 5:52
  • 1
    You're missing the question. I know how to sort, that's not what I'm asking but thanks anyway
    – healix
    Apr 4, 2013 at 6:00
0

You have two options.

  1. Create a new sortable name column with the spaces removed, sort by this temporary/new column, and then finally suppress the output of this "temporary" column, or

  2. Ensure that the Field separator is not a valid character in any field. A Space is a valid character in the City Name, so you have to use something else for field separator.

Option 1 allows you some more control, because you can substitute the names with any other names, eg all lower case, removed numericals, convert characters to other characters, and so on, but also the more complex to implement.

Option 2 can be done like this:

#!/bin/bash
awk '{TLOW=$(NF-1); 
      THIGH=$NF; 
      $NF=""; 
      $(NF-1)="";
      gsub(" +$","");
      CITYNM=$0; 
      printf ("%s:%s:%s\n", CITYNM, TLOW, THIGH)}' | sort -t: 

Now the question is how you want to sort a name like "Cape Town" versus "Capelle". The unix sort command puts Capelle before Cape Town, but that might sometimes not be what you want, which is when option 1 comes into play.

Not the answer you're looking for? Browse other questions tagged .