0

E.g. here is a sample of the file below. I'd like to sort all the lines in order of Corr, where the delimiter before the number is "=" and the delimiter after the number is "at".

PrecipNH0to90vsNetNH0to90 Corr = -0.5073 at Net Leading Precip by -1 Months Time Lag
PrecipNH0to90vsNetSH0to90 Corr = -0.6498 at Net Leading Precip by 2 Months Time Lag
PrecipNH0to90vsNetHemDif0to90 Corr = 0.66939 at Net Leading Precip by 9 Months Time Lag
PrecipNH0to90vsNetGlobal0to90 Corr = -0.66036 at Net Leading Precip by 0 Months Time Lag
PrecipNH0to90vsNetAsymIndex0to90 Corr = 0.65726 at Net Leading Precip by 0 Months Time Lag
PrecipNH0to90vsNetNH0to14 Corr = -0.46212 at Net Leading Precip by -2 Months Time Lag
PrecipNH0to90vsNetSH0to14 Corr = -0.70731 at Net Leading Precip by 4 Months Time Lag
PrecipNH0to90vsNetHemDif0to14 Corr = 0.70494 at Net Leading Precip by 8 Months Time Lag
PrecipNH0to90vsNetGlobal0to14 Corr = -0.66121 at Net Leading Precip by 0 Months Time Lag
PrecipNH0to90vsNetAsymIndex0to14 Corr = 0.64884 at Net Leading Precip by 8 Months Time Lag
PrecipNH0to90vsNetNH14to30 Corr = 0.46232 at Net Leading Precip by 10 Months Time Lag
PrecipNH0to90vsNetSH14to30 Corr = -0.80044 at Net Leading Precip by 2 Months Time Lag
PrecipNH0to90vsNetHemDif14to30 Corr = 0.74188 at Net Leading Precip by 9 Months Time Lag
PrecipNH0to90vsNetGlobal14to30 Corr = -0.62494 at Net Leading Precip by 2 Months Time Lag
PrecipNH0to90vsNetAsymIndex14to30 Corr = 0.46709 at Net Leading Precip by 5 Months Time Lag
PrecipNH0to90vsNetNH30to49 Corr = 0.49765 at Net Leading Precip by 10 Months Time Lag
PrecipNH0to90vsNetSH30to49 Corr = 0.21001 at Net Leading Precip by 10 Months Time Lag

I know that the file could be organized a lot more neatly when I printed it out from Matlab, but I'm still curious about this as a general case.

1
  • 1
    Looks to me that the delimiter is SPACE and you want to sort on column 4 using the sort command, not awk
    – Totoro
    May 21, 2014 at 4:08

1 Answer 1

5

Try this:

sort -nk4,4 <filename>

Or if you REALLY love awk:

awk '{print $4}' <filename> | sort -n

sort -nk4 = sort numerically (n) on the 4th field only (k4,4)

awk - {print $4} = print the 4th field only. Awk automatically splits by spaces.

Finally, just for fun I did a version that uses ONLY awk to implement it's own bubble sort. :-) It could probably be a little bit cleaner, but it works.

#!/usr/bin/awk -f
# Script to sort a data file based on column 4
{
  # Read every line into an array
  line[NR]  = $0
  # Also save the sort column so we don't have to split the line again repeatedly
  value[NR] = $4
}
END { # sort it with bubble sort
  do {
    haschanged = 0
    for(i=1; i < NR; i++) {
      if ( value[i] > value[i+1] ) {
        # Swap adjacent lines and values.
        t = line[i]
        line[i] = line[i+1]
        line[i+1] = t
        t = value[i]
        value[i] = value[i+1]
        value[i+1] = t
        haschanged = 1
      }
    }
  } while ( haschanged == 1 )
  # Print out the result.
  for(i=1; i <= NR; i++) {
    print line[i]
  }
}
1
  • 3
    sort -k4 doesn't sort based on the 4th field (only) - it sorts FROM the 4th field THROUGH THE END OF THE LINE. Because of -n (numerical sorting), it happens to be limited to the 4th field in this case, because parsing the remainder of the line is only performed as far as the string is recognized as a number. Generally, to truly only sort by field 4, use sort -k4,4. With GNU sort you can see this behavior in action with sort --debug -k4 vs. sort --debug -k4,4. sort is a strange beast.
    – mklement0
    May 21, 2014 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.