Unix Shell嵌套循环-语法错误

时间:2018-10-29 13:12:26

标签: shell loops for-loop unix awk

输入文件格式

var array_a = [2, 3, 4, 1, 5];
var array_b = [0, 7, 9, 8, 6];
var array_c = ['A', 'D', 'B', 'A', 'E'];

// get indices from array_a sorted
var len = array_a.length;
var indices = new Array(len);
for (var i = 0; i < array_a.length; ++i) indices[i] = i;
indices.sort(function(a, b) {
  return array_a[a] < array_a[b] ? -1 : array_a[a] > array_a[b] ? 1 : 0;
});

// sorted array_b and sorted_array_c
var sorted_array_a = [];
var sorted_array_b = [];
var sorted_array_c = [];
for (var i = 0; i < indices.length; ++i) {
  sorted_array_a.push(array_a[indices[i]]);
  sorted_array_b.push(array_b[indices[i]]);
  sorted_array_c.push(array_c[indices[i]])
}

var multi_array = [sorted_array_a, sorted_array_b, sorted_array_c]


console.log(multi_array);

我正在尝试修复语法错误。以下代码将输出正确打印。

T12345670|1966-04-21|A01|1922-04-21|somebody name    | |9999-01-01| 
|CSGE|P175           |AGENT    |AMGDHR01|1977-04-21|1977-04-21|2018-10-05|1977-04-21|0001-01-01|          |          |2018-11-01|2019-04-21| 2019.|01|D|M|PMI |D|0|N| 1
.| 1.|N| 1.| 1.|0|       |   ||N|0001-01-01|   | 000000143.04| 000000075.00| 000000011.92| 000000011.92| 000000011.92| 000000034.14| 000000066.97| 000000130.04| 000005946.16|0|2018-10-05| 000000000.00|Y|2018-10-24|2018-10-23|499| 01.|||

打印此内容:

awk -F'|' -v OFS=", " '{$61!=""} {k=substr($61,1,1); ks[k]; c[k,length($61)]++} END {for(k in ks) print k": "c[k,0],c[k,1],c[k,2],c[k,3],c[k,4],c[k,5],c[k,6],c[k,7],c[k,8],c[k,9],c[k,10],c[k,11],c[k,12],c[k,13],c[k,14],c[k,15]}' The_File.sqf | sort

很酷,对吧?

问题:现在,我正在尝试使用NESTED FOR LOOP清理该代码,但是在尝试许多变体后却出现语法错误。

0: , , , , , , , , , , 2010, , , , , 3010
1: , , , , , , , , , , 40, , , , , 44
3: , , , , , , , , , , 5, , , , , 1
4: , , , , , , , , , , 6, , , , , 3
5: , , , , , , , , , , 212, , , , , 92
7: , , , , , , , , , , 79, , , , ,
8: , , , , , , , , , , 966, , , , ,
: 1279330, , , , , , , , , , , , , , ,
C: , , , , , , , , , , , , , , , 2198

错误:

awk -F'|' -v OFS=", " '{$61!=""} {k=substr($61,1,1); ks[k]; c[k,length($61)]++} END {for(k in ks); do for(i in {1..15}); do print k": " c[k,i]}; done; done' TheFile.sqf | sort

1 个答案:

答案 0 :(得分:0)

在这里我为您修复了该问题{$61!=""}并不意味着什么,因此将其删除。

$ awk -F'|' '{k=substr($61,1,1); ks[k]; 
              c[k,length($61)]++} 
         END {for(k in ks)
                {printf "%s: ", k;
                 for(i=1;i<=15;i++) printf ", %d", c[k,i];
                 print ""}}' file | sort