AWK-用字符串替换特定范围内的值

AWK- replace values on specific ranges with strings

我有一个名为“差异”的列($11),它是一场比赛中两支球队之间的最终差异。

我有很多值,所以我想简化用户的阅读。

假设如果差异大于-20,则该值应替换为“大损失”。如果它在 -19 和 -5 之间,请替换为“损失”。如果它在 -4 和 -1 之间,则替换为“close loss”

同样的逻辑,但有正数(接近赢,赢,大赢)。

我想我知道在 bash 中该由谁来做,但是 AWK 对我来说是新事物(我在 AWK 中需要它)。

在bash我认为可能是:

IFS=',' records=() sorted=()
{ 
    IFS='' read -r header

    while read -r -a values
    do

        case ${values[11]} in
            (${values[11]}>-20)) values[11]=Big Loss ;;
            (-20>${values[11]}>-5)) values[11]=Loss ;;
            (-5>${values[11]}>0)) values[11]=Close Loss ;;
            (${values[11]}>20)) values[11]=Big Win ;;
            (20>${values[11]}>5)) values[11]=Win ;;
            (5>${values[11]}>0))) values[11]=Close Win ;;
    done

一些输入数据:

    EndYear,Rk,G,Date,Years,Days,Age,Tm,Home,Opp,Win,Diff,GS,MP,FG,FGA,FG_PCT,3P,3PA,3P_PCT,FT,FTA,FT_PCT,ORB,DRB,TRB,AST,STL,BLK,TOV,PF,PTS,GmSc
    1985,1,1,10/26/1984,21,252,21.6899384,CHI,1,WSB,1,16,1,40,5,16,0.313,0,0,,6,7,0.857,1,5,6,7,2,4,5,2,16,12.5
    1985,2,2,10/27/1984,21,253,21.69267625,CHI,0,MIL,0,-2,1,34,8,13,0.615,0,0,,5,5,1,3,2,5,5,2,1,3,4,21,19.4
    1985,3,3,10/29/1984,21,255,21.69815195,CHI,1,MIL,1,6,1,34,13,24,0.542,0,0,,11,13,0.846,2,2,4,5,6,2,3,4,37,32.9
    1985,4,4,10/30/1984,21,256,21.7008898,CHI,0,KCK,1,5,1,36,8,21,0.381,0,0,,9,9,1,2,2,4,5,3,1,6,5,25,14.7
    1985,5,5,11/1/1984,21,258,21.7063655,CHI,0,DEN,0,-16,1,33,7,15,0.467,0,0,,3,4,0.75,3,2,5,5,1,1,2,4,17,13.2
    1985,6,6,11/7/1984,21,264,21.72279261,CHI,0,DET,1,4,1,27,9,19,0.474,0,0,,7,9,0.778,1,3,4,3,3,1,5,5,25,14.9
    1985,7,7,11/8/1984,21,265,21.72553046,CHI,0,NYK,1,15,1,33,15,22,0.682,0,0,,3,4,0.75,4,4,8,5,3,2,5,2,33,29.3
    1985,8,8,11/10/1984,21,267,21.73100616,CHI,0,IND,1,2,1,42,9,22,0.409,0,0,,9,12,0.75,2,7,9,4,2,5,3,4,27,21.2
    1985,9,9,11/13/1984,21,270,21.73921971,CHI,1,SAS,1,3,1,43,18,27,0.667,1,1,1,8,11,0.727,2,8,10,4,3,2,4,4,45,37.5
    1985,10,10,11/15/1984,21,272,21.74469541,CHI,1,BOS,0,-20,1,33,12,24,0.5,0,1,0,3,3,1,0,2,2,2,2,1,1,4,27,17.1
    1985,11,11,11/17/1984,21,274,21.75017112,CHI,1,PHI,0,-9,1,44,4,17,0.235,0,0,,8,8,1,0,5,5,7,5,2,4,5,16,12.5
    1985,12,12,11/19/1984,21,276,21.75564682,CHI,1,IND,0,-17,1,39,11,26,0.423,0,3,0,12,16,0.75,2,3,5,2,2,1,3,3,34,20.8
    1985,13,13,11/21/1984,21,278,21.76112252,CHI,0,MIL,0,-10,1,42,11,22,0.5,0,0,,13,14,0.929,4,9,13,2,2,2,6,3,35,26.7
    1985,14,14,11/23/1984,21,280,21.76659822,CHI,0,SEA,1,19,1,30,9,13,0.692,0,0,,5,6,0.833,0,4,4,3,4,1,4,4,23,19.5
    1985,15,15,11/24/1984,21,281,21.76933607,CHI,0,POR,0,-10,1,41,10,24,0.417,0,1,0,10,10,1,3,3,6,8,3,1,4,4,30,23.9

预期输出:

 EndYear,Rk,G,Date,Years,Days,Age,Tm,Home,Opp,Win,Diff,GS,MP,FG,FGA,FG_PCT,3P,3PA,3P_PCT,FT,FTA,FT_PCT,ORB,DRB,TRB,AST,STL,BLK,TOV,PF,PTS,GmSc
    1985,1,1,10/26/1984,21,252,21.6899384,CHI,1,WSB,1,Win,1,40,5,16,0.313,0,0,,6,7,0.857,1,5,6,7,2,4,5,2,16,12.5
    1985,2,2,10/27/1984,21,253,21.69267625,CHI,0,MIL,0,Close Loss,1,34,8,13,0.615,0,0,,5,5,1,3,2,5,5,2,1,3,4,21,19.4
    1985,3,3,10/29/1984,21,255,21.69815195,CHI,1,MIL,1,Win,1,34,13,24,0.542,0,0,,11,13,0.846,2,2,4,5,6,2,3,4,37,32.9
    1985,4,4,10/30/1984,21,256,21.7008898,CHI,0,KCK,1,Win,1,36,8,21,0.381,0,0,,9,9,1,2,2,4,5,3,1,6,5,25,14.7
    1985,5,5,11/1/1984,21,258,21.7063655,CHI,0,DEN,0,Loss,1,33,7,15,0.467,0,0,,3,4,0.75,3,2,5,5,1,1,2,4,17,13.2
    1985,6,6,11/7/1984,21,264,21.72279261,CHI,0,DET,1,Close Win,1,27,9,19,0.474,0,0,,7,9,0.778,1,3,4,3,3,1,5,5,25,14.9
    1985,7,7,11/8/1984,21,265,21.72553046,CHI,0,NYK,1,Win,1,33,15,22,0.682,0,0,,3,4,0.75,4,4,8,5,3,2,5,2,33,29.3
    1985,8,8,11/10/1984,21,267,21.73100616,CHI,0,IND,1,Close Win,1,42,9,22,0.409,0,0,,9,12,0.75,2,7,9,4,2,5,3,4,27,21.2
    1985,9,9,11/13/1984,21,270,21.73921971,CHI,1,SAS,1,Close Win,1,43,18,27,0.667,1,1,1,8,11,0.727,2,8,10,4,3,2,4,4,45,37.5
    1985,10,10,11/15/1984,21,272,21.74469541,CHI,1,BOS,0,Big Loss,1,33,12,24,0.5,0,1,0,3,3,1,0,2,2,2,2,1,1,4,27,17.1
    1985,11,11,11/17/1984,21,274,21.75017112,CHI,1,PHI,0,Loss,1,44,4,17,0.235,0,0,,8,8,1,0,5,5,7,5,2,4,5,16,12.5
    1985,12,12,11/19/1984,21,276,21.75564682,CHI,1,IND,0,Loss,1,39,11,26,0.423,0,3,0,12,16,0.75,2,3,5,2,2,1,3,3,34,20.8
    1985,13,13,11/21/1984,21,278,21.76112252,CHI,0,MIL,0,Loss,1,42,11,22,0.5,0,0,,13,14,0.929,4,9,13,2,2,2,6,3,35,26.7
    1985,14,14,11/23/1984,21,280,21.76659822,CHI,0,SEA,1,Win,1,30,9,13,0.692,0,0,,5,6,0.833,0,4,4,3,4,1,4,4,23,19.5
    1985,15,15,11/24/1984,21,281,21.76933607,CHI,0,POR,0,Loss,1,41,10,24,0.417,0,1,0,10,10,1,3,3,6,8,3,1,4,4,30,23.9

Assumptions/Understandings:

  • 根据示例数据文件,Diff 列是字段 #12 (</code>)</li> <li>if <code>Diff == 0 我们将分配 Tie
  • 的字符串

设置: 将第一个数据行修改为 Diff == 0 以演示 Tie 测试

一个awk想法:

awk '                                             # do not include this line if placing in a script file (eg, script.awk)
BEGIN { FS=OFS="," }
FNR>1 {      if ( <= -20)  = "Big Loss"
        else if ( <=  -5)  = "Loss"
        else if ( <=  -1)  = "Close Loss"
        else if ( <=   0)  = "Tie"
        else if ( <=   4)  = "Close Win"
        else if ( <=  19)  = "Win"
        else                  = "Big Win"
      }
      { print }
' scores.dat                                      # do not include this line if placing in a script file (eg, script.awk)

另一个 awk 使用数组保存边界值和相关替换字符串的想法:

awk '                                             # do not include this line if placing in a script file (eg, script.awk)
BEGIN { FS=OFS=","
        n=split("-20,-5,-1,0,4,19,9999999",boundary,",")
          split("Big Loss,Loss,Close Loss,Tie,Close Win,Win,Big Win",replace,",")
      }
FNR>1 { for (i=1;i<=n;i++)
            if ( <= boundary[i]) {
                = replace[i]
               break
            }
      }
      { print }
' scores.dat                                      # do not include this line if placing in a script file (eg, script.awk)

这两个都会产生:

EndYear,Rk,G,Date,Years,Days,Age,Tm,Home,Opp,Win,Diff,GS,MP,FG,FGA,FG_PCT,3P,3PA,3P_PCT,FT,FTA,FT_PCT,ORB,DRB,TRB,AST,STL,BLK,TOV,PF,PTS,GmSc
1985,1,1,10/26/1984,21,252,21.6899384,CHI,1,WSB,1,Tie,1,40,5,16,0.313,0,0,,6,7,0.857,1,5,6,7,2,4,5,2,16,12.5
1985,2,2,10/27/1984,21,253,21.69267625,CHI,0,MIL,0,Close Loss,1,34,8,13,0.615,0,0,,5,5,1,3,2,5,5,2,1,3,4,21,19.4
1985,3,3,10/29/1984,21,255,21.69815195,CHI,1,MIL,1,Win,1,34,13,24,0.542,0,0,,11,13,0.846,2,2,4,5,6,2,3,4,37,32.9
1985,4,4,10/30/1984,21,256,21.7008898,CHI,0,KCK,1,Win,1,36,8,21,0.381,0,0,,9,9,1,2,2,4,5,3,1,6,5,25,14.7
1985,5,5,11/1/1984,21,258,21.7063655,CHI,0,DEN,0,Loss,1,33,7,15,0.467,0,0,,3,4,0.75,3,2,5,5,1,1,2,4,17,13.2
1985,6,6,11/7/1984,21,264,21.72279261,CHI,0,DET,1,Close Win,1,27,9,19,0.474,0,0,,7,9,0.778,1,3,4,3,3,1,5,5,25,14.9
1985,7,7,11/8/1984,21,265,21.72553046,CHI,0,NYK,1,Win,1,33,15,22,0.682,0,0,,3,4,0.75,4,4,8,5,3,2,5,2,33,29.3
1985,8,8,11/10/1984,21,267,21.73100616,CHI,0,IND,1,Close Win,1,42,9,22,0.409,0,0,,9,12,0.75,2,7,9,4,2,5,3,4,27,21.2
1985,9,9,11/13/1984,21,270,21.73921971,CHI,1,SAS,1,Close Win,1,43,18,27,0.667,1,1,1,8,11,0.727,2,8,10,4,3,2,4,4,45,37.5
1985,10,10,11/15/1984,21,272,21.74469541,CHI,1,BOS,0,Big Loss,1,33,12,24,0.5,0,1,0,3,3,1,0,2,2,2,2,1,1,4,27,17.1
1985,11,11,11/17/1984,21,274,21.75017112,CHI,1,PHI,0,Loss,1,44,4,17,0.235,0,0,,8,8,1,0,5,5,7,5,2,4,5,16,12.5
1985,12,12,11/19/1984,21,276,21.75564682,CHI,1,IND,0,Loss,1,39,11,26,0.423,0,3,0,12,16,0.75,2,3,5,2,2,1,3,3,34,20.8
1985,13,13,11/21/1984,21,278,21.76112252,CHI,0,MIL,0,Loss,1,42,11,22,0.5,0,0,,13,14,0.929,4,9,13,2,2,2,6,3,35,26.7
1985,14,14,11/23/1984,21,280,21.76659822,CHI,0,SEA,1,Win,1,30,9,13,0.692,0,0,,5,6,0.833,0,4,4,3,4,1,4,4,23,19.5
1985,15,15,11/24/1984,21,281,21.76933607,CHI,0,POR,0,Loss,1,41,10,24,0.417,0,1,0,10,10,1,3,3,6,8,3,1,4,4,30,23.9