【问题标题】:Find matching index based on multiple files and print根据多个文件查找匹配索引并打印
【发布时间】:2018-05-24 05:23:32
【问题描述】:

我有以下 3 个文件,所有 3 个文件的列数和行数都相同(超过数百个)。我想要的是:如果 File1 和 File2 中的数字都在特定范围内,则找到 col/row,然后将 File3 中的数字保持相同的索引并将“0”标记为其他数字。 eg:从File1 和File2 中,只有col2/row2 处的数字可以满足标准(0

文件1:

-10 -10 9 
-20 88 106 
-30 300 120

文件2:

-6 0 -7
-5 6 1
-2 18 32

文件3:

4 3 5 
6 8 8
10 23 14

输出

0 0 0
0 8 0
0 0 0

【问题讨论】:

  • 好问题,也尝试添加更明确的条件(在这种情况下为范围)+您解决问题的努力,我们都在这里学习。

标签: indexing awk matching


【解决方案1】:

关注awk 会有所帮助。

awk '
FNR==1                 { count++             }  ##Checking condition if FNR==1 then increment variable count with 1 each time.
count==1               {                        ##Checking condition if count is either 1 or 2 if yes then do following.
   for(i=1;i<=NF;i++)  {                        ##Starting a usual for loop from variable value 1 to till value of NF here and doing following.
     if($i>0 && $i<100){ a[FNR,i]++          }  ##Checking condition if a field value is greater than 0 and lesser than 100 then increment 1 count for array a whose index is line_number and column_number here. So this will have the record of which ever line whichever column has values in range and if count is 2 then we should print it.
}}
count==2               {
   for(i=1;i<=NF;i++)  {
     if($i>0 && $i<10) { a[FNR,i]++          }
}}
count==3               {                        ##Checking condition if variable count is 3 here then do following.
   for(j=1;j<=NF;j++)  { $j=a[FNR,j]==2?$j:0 }; ##Starting a for loop here from 1 to till NF value and checking condition if array a with index of line_number and column_number is 2(means both File1 and File2 have same ranges) then keep its same value else make it 0 as per OP request.
   print                                     }  ##Printing the current line edited/non-edited value here.
' File1 File2 File3                             ##Mentioning all Input_file(s) here.

输出如下。

0 0 0
0 8 0
0 0 0

【讨论】:

  • 感谢您的帮助!我知道 $i>0 && $i
  • 感谢您花时间提供解释!但是 File2 ($i>0 && $i0 && $i
  • @kelly,好吧,现在更改代码很酷,检查并告诉我一切是否正常??
【解决方案2】:

你有一个很棒的awk 答案。

以下是在 Python 中使用 numpy 执行此操作的方法。

首先,读取文件:

import numpy as np
arrays=[]
for fn in ('file1', 'file2', 'file3'):
    with open(fn) as f:
        arrays.append(np.array([line.split() for line in f],dtype=float))

然后创建一个掩码矩阵来过滤所需的条件:

mask=(arrays[0]>0) & (arrays[0]<100) & (arrays[1]>0) & (arrays[1]<10)

然后将第三个数组(arrays[2] 是第三个文件)乘以掩码:

>>> arrays[2] * mask.astype(float)
[[0. 0. 0.]
 [0. 8. 0.]
 [0. 0. 0.]]

【讨论】:

    猜你喜欢
    • 2013-03-21
    • 2018-11-11
    • 2019-05-04
    • 2021-01-08
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多