如何使用awk按两个常用分隔符包围的数字对列表进行排序?

时间:2014-05-21 04:05:02

标签: unix awk

E.g。这是下面文件的示例。我想按照Corr的顺序对所有行进行排序,其中数字之前的分隔符是" ="数字之后的分隔符为" 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

我知道当我从Matlab打印出来时,文件可以更整齐地组织起来,但我仍然对此作为一般情况感到好奇。

1 个答案:

答案 0 :(得分:5)

试试这个:

sort -nk4,4 <filename>

或者如果你真的爱awk:

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

sort -nk4 =仅在第4个字段上以数字(n)排序(k4,4)

awk - {print $ 4} =仅打印第4个字段。 Awk会自动按空格分割。

最后,为了好玩,我做了一个只使用awk来实现它自己的冒泡排序的版本。 :-)它可能有点干净,但它确实有效。

#!/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]
  }
}