如何根据创建的列对输出进行排序



我创建了一个awk文件,用于对csv文件中的一些数据进行排序。以下是的数据片段

|Timestamp     |Email                    |Name      |Year|Make    |Model    |Car_ID|Judge_ID|Judge_Name|Racer_Turbo|Racer_Supercharged|Racer_Performance|Racer_Horsepower|Car_Overall|Engine_Modifications|Engine_Performance|Engine_Chrome|Engine_Detailing|Engine_Cleanliness|Body_Frame_Undercarriage|Body_Frame_Suspension|Body_Frame_Chrome|Body_Frame_Detailing|Body_Frame_Cleanliness|Mods_Paint|Mods_Body|Mods_Wrap|Mods_Rims|Mods_Interior|Mods_Other|Mods_ICE|Mods_Aftermarket|Mods_WIP|Mods_Overall|
|--------------|-------------------------|----------|----|--------|---------|------|--------|----------|-----------|------------------|-----------------|----------------|-----------|--------------------|------------------|-------------|----------------|------------------|------------------------|---------------------|-----------------|--------------------|----------------------|----------|---------|---------|---------|-------------|----------|--------|----------------|--------|------------|
|8/5/2018 14:10|honoland13@japanpost.jp  |Hernando  |2015|Acura   |TLX      |48    |J04     |Bob       |0          |0                 |2                |2               |4          |4                   |0                 |2            |4               |4                 |2                       |4                    |2                |2                   |2                     |2         |2        |0        |4        |4            |4         |6       |2               |0       |4           |
|8/5/2018 15:11|nlighterness2q@umn.edu   |Noel      |2015|Jeep    |Wrangler |124   |J02     |Carl      |0          |6                 |4                |2               |4          |6                   |6                 |4            |4               |4                 |6                       |6                    |6                |6                   |6                     |4         |6        |6        |6        |6            |6         |4       |6               |4       |6           |
|8/5/2018 17:10|eguest47@microsoft.com   |Edan      |2015|Lexus   |Is250    |222   |J05     |Adrian    |0          |0                 |0                |0               |0          |0                   |0                 |0            |6               |6                 |6                       |0                    |0                |6                   |6                     |6         |0        |0        |0        |0            |0         |0       |0               |0       |4           |
|8/5/2018 17:34|hchilley40@fema.gov      |Hieronymus|1993|Honda   |Civic eG |207   |J06     |Aaron     |0          |0                 |2                |2               |2          |2                   |2                 |2            |0               |4                 |2                       |2                    |2                |2                   |2                     |2         |4        |2        |2        |0            |0         |0       |2               |2       |0           |
|8/5/2018 14:30|nnowick3d@tuttocitta.it  |Nickolas  |2016|Ford    |Mystang  |167   |J02     |Carl      |0          |0                 |2                |2               |0          |2                   |2                 |0            |0               |0                 |0                       |2                    |0                |2                   |2                     |2         |0        |0        |2        |0            |0         |0       |0               |0       |2           |
|8/5/2018 16:12|mdearl39@amazon.co.uk    |Martin    |2013|Hyundai |Gen coupe|159   |J04     |Bob       |0          |0                 |2                |0               |0          |0                   |2                 |0            |0               |0                 |0                       |2                    |0                |2                   |2                     |0         |2        |0        |2        |0            |0         |0       |0               |0       |0           |
|8/5/2018 17:00|alynamg@blogtalkradio.com|Aldridge  |2009|Infiniti|G37      |20    |J06     |Aaron     |2          |0                 |2                |2               |0          |0                   |2                 |0            |0               |2                 |2                       |2                    |2                |2                   |2                     |2         |2        |2        |4        |2            |2         |0       |2               |0       |2           |
|8/5/2018 16:11|abowton3k@spiegel.de     |Ambros    |2009|Honda   |Oddesy   |178   |J06     |Aaron     |2          |0                 |2                |2               |2          |2                   |2                 |0            |4               |4                 |2                       |2                    |2                |4                   |4                     |4         |2        |2        |         |6            |4         |4       |6               |4       |6           |

我能够产生的输出数据看起来像这个

Ranking   Car_ID   Year    Make      Model      Total
1         48       2015    Acura     TLX        62   
2         124      2015    Jeep      Wrangler   124   
3         222      2015    Lexus     Is250      40  
...

我希望能够根据total列按降序对上面的输出进行排序,但我不知道如何在awk中这样做。合计和排名列不是原始csv数据的一部分,仅在输出过程中产生。这是我到目前为止的代码

BEGIN {
FS = ",";
OFS = "tt";
}
NR==1 {
$35 = "Ranking";
$36 = "Total";
}
NR>1 {
$35 = 1;
for(i = 1; i < NR - 1;  i++) { $35 += 1 }
$36 = $10 + $11 + $12 + $13 + $14 + $15 + $16 + $17 + $18 + $19 + $20 + $21 + $22 + $23 + $24 + $25 + $26 + $27 + $28 + $29 + $30 + $31 + $32 + $33 + $34;
}
{
print $35, $7, $4, $5, $6, $36;
}

当我运行这个并添加"|排序-nk36|";在命令结束时,它似乎不会以任何方式更改输出或对其进行排序。也许我的命令被弄糊涂了。

预期输出应该类似于

Ranking    Car_ID    Year     Make      Model       Total
1          48        2015     Jeep      Wrangler    124  
2          124       2015     Acura     TLX         62   
3          222       2015     Lexus     Is250       40   

假设:

  • 输入字段是逗号分隔的(虽然OP的示例输入显示为具有管道边界的固定宽度,但OP的awk代码规定了"FS=",",并且由于OP声称awk代码正在运行并生成输出,我们将使用FS=","(
  • OP的样本输入中的第二行(实线连字符(实际上并不存在于OP的文件中(事实上,OP的awk代码并不为NR==2编码(
  • 输出将以制表符分隔(当OP的awk代码提到OFS="tt"时,示例输出似乎是…固定宽度?(
  • Ranking分配基于排序结果(即,不基于OP的awk代码中所示的输入排序(

设置:

$ cat raw.dat
Timestamp,Email,Name,Year,Make,Model,Car_ID,Judge_ID,Judge_Name,Racer_Turbo,Racer_Supercharged,Racer_Performance,Racer_Horsepower,Car_Overall,Engine_Modifications,Engine_Performance,Engine_Chrome,Engine_Detailing,Engine_Cleanliness,Body_Frame_Undercarriage,Body_Frame_Suspension,Body_Frame_Chrome,Body_Frame_Detailing,Body_Frame_Cleanliness,Mods_Paint,Mods_Body,Mods_Wrap,Mods_Rims,Mods_Interior,Mods_Other,Mods_ICE,Mods_Aftermarket,Mods_WIP,Mods_Overall
8/5/2018 14:10,honoland13@japanpost.jp,Hernando,2015,Acura,TLX,48,J04,Bob,0,0,2,2,4,4,0,2,4,4,2,4,2,2,2,2,2,0,4,4,4,6,2,0,4
8/5/2018 15:11,nlighterness2q@umn.edu,Noel,2015,Jeep,Wrangler,124,J02,Carl,0,6,4,2,4,6,6,4,4,4,6,6,6,6,6,4,6,6,6,6,6,4,6,4,6
8/5/2018 17:10,eguest47@microsoft.com,Edan,2015,Lexus,Is250,222,J05,Adrian,0,0,0,0,0,0,0,0,6,6,6,0,0,6,6,6,0,0,0,0,0,0,0,0,4
8/5/2018 17:34,hchilley40@fema.gov,Hieronymus,1993,Honda,CiviceG,207,J06,Aaron,0,0,2,2,2,2,2,2,0,4,2,2,2,2,2,2,4,2,2,0,0,0,2,2,0
8/5/2018 14:30,nnowick3d@tuttocitta.it,Nickolas,2016,Ford,Mystang,167,J02,Carl,0,0,2,2,0,2,2,0,0,0,0,2,0,2,2,2,0,0,2,0,0,0,0,0,2
8/5/2018 16:12,mdearl39@amazon.co.uk,Martin,2013,Hyundai,Gencoupe,159,J04,Bob,0,0,2,0,0,0,2,0,0,0,0,2,0,2,2,0,2,0,2,0,0,0,0,0,0
8/5/2018 17:00,alynamg@blogtalkradio.com,Aldridge,2009,Infiniti,G37,20,J06,Aaron,2,0,2,2,0,0,2,0,0,2,2,2,2,2,2,2,2,2,4,2,2,0,2,0,2
8/5/2018 16:11,abowton3k@spiegel.de,Ambros,2009,Honda,Oddesy,178,J06,Aaron,2,0,2,2,2,2,2,0,4,4,2,2,2,4,4,4,2,2,,6,4,4,6,4,6

一个GNU awk(用于支持PROCINFO["sorted_in"](想法:

awk '
BEGIN  { FS=","; OFS="t" }
FNR==1 { print "Ranking",$7,$4,$5,$6,"Total"; next }
{ totals[FNR]=0
for (i=10;i<=34;i++)
totals[FNR]+= $i
lines[FNR]=$7 OFS $4 OFS $5 OFS $6
}
END    { PROCINFO["sorted_in"]="@val_num_desc"            # sort totals[] array by numeric value (descending order)
ranking=0
for (i in totals)                                # loop through indices of the totals[] array
print ++ranking,lines[i],totals[i]
}
' raw.dat

这将生成:

Ranking Car_ID  Year    Make    Model   Total
1       124     2015    Jeep    Wrangler        124
2       178     2009    Honda   Oddesy  72
3       48      2015    Acura   TLX     62
4       207     1993    Honda   CiviceG 40
5       222     2015    Lexus   Is250   40
6       20      2009    Infiniti        G37     38
7       167     2016    Ford    Mystang 20
8       159     2013    Hyundai Gencoupe        14

如果OP需要打印出所有列都排列整齐的输出,那么这可以用更多的代码来完成,或者我们可以通过column(假设MakeModel列中没有嵌入空格(来传输结果,例如:

$ awk 'BEGIN ... ' raw.dat | column -t
Ranking  Car_ID  Year  Make      Model     Total
1        124     2015  Jeep      Wrangler  124
2        178     2009  Honda     Oddesy    72
3        48      2015  Acura     TLX       62
4        207     1993  Honda     CiviceG   40
5        222     2015  Lexus     Is250     40
6        20      2009  Infiniti  G37       38
7        167     2016  Ford      Mystang   20
8        159     2013  Hyundai   Gencoupe  14

注意:

  • 对于Total列中有重复值的情况,没有提供额外的排序要求,因此我们将按awkEND/for循环中处理数据的任何顺序打印

相关内容

  • 没有找到相关文章

最新更新