我有以下DATA-gsub多列同时基于不同的gsub条件?
输入 -
A B C D E F
A B B B B B
C A C D E F
A B D E F A
A A A A A F
A B C B B B
文件如果任何从第2行具有相同的信作为第1行开始与其他行的,它们应改为1基本上,我试图找出如何相似的行是第一行。希望的输出 -
1 1 1 1 1 1
1 1 B B B B
C A 1 1 1 1
1 1 D E F A
1 A A A A 1
1 1 1 B B B
的第一行已经成为所有1,因为它是与自身(明显)。在第二行中,第一列和第二列与第一行相同(A B
),因此它们变为1 1
。等其他行。
我写了下面的代码执行此transformation-
for seq in {1..1} ; #Iterate over the rows (in this case just row 1)
do
for position in {1..6} ; #Iterate over the columns
do
#Define the letter in the first row with which I'm comparing the rest of the rows
aa=$(awk -v pos=$position -v line=$seq 'NR == line {print $pos}' f)
#If it matches, gsub it to 1
awk -v var=$aa -v pos=$position '{gsub (var, "1", $pos)} 1' f > temp
#Save this intermediate file and now act on this
mv temp f
done
done
你可以想像,这实在是太慢了,因为嵌套循环是昂贵的。我的真实数据是一个60x10000的矩阵,它需要大约2个小时才能运行该程序。
我希望你能帮我摆脱内部循环,这样我就可以一步完成所有6个gsub。也许把它们放在他们自己的数组中?我的awk
技能还没那么好。
请看看:我该怎么办时,有人回答我的问题?(http://stackoverflow.com/help/someone-answers) – Cyrus