【问题标题】:Comparing specific column from a file to another file with specified number of times以指定的次数将文件中的特定列与另一个文件进行比较
【发布时间】:2017-12-08 14:15:05
【问题描述】:

我有 2 个文件,分别是 file1.txt 和 file2.txt。

file1.txt:

2017042100000000010000000943678177700000000819900000026572
2017042100000000010000000943678177700000003500000000026581
2017042100000000010000000943678177700000013450000000026591
2017042100000000010000000943678177700000011500000000026601
2017042100000000010000000943678177700000010000000000026611
2017042100000000010000000943678177700000010000000000026622
2017042100000000010000000943678177700000012855000000026632

file2.txt

20170421,0000000001,00000009436781777,000000008199,0000002657,F,3,img_F1_1.tiff
20170421,0000000001,00000009436781777,000000008199,0000002657,B,3,img_F1_1.tiff
20170421,0000000001,00000009436781777,000000035000,0000002658,F,8,img_F1_2.tiff
20170421,0000000001,00000009436781777,000000134500,0000002659,F,1,img_F1_3.tiff
20170421,0000000001,00000009436781777,000000115000,0000002660,F,2,img_F1_4.tiff
20170421,0000000001,00000009436781777,000000100000,0000002661,F,1,img_F1_5.tiff
20170421,0000000001,00000009436781777,000000100000,0000002662,F,8,img_F1_6.tiff

我必须将 file1.txt 的条目(最后一个字符除外)与 file2.txt 的前 5 列进行比较。如果匹配,那么我必须将 file2.txt 的条目存储到另一个文件中,比如matched.txt。如果没有,那么我必须将 file1.txt 的条目存储在另一个文件中,比如 unmatched.txt。使用以下命令对我有用。

awk -F',' 'FILENAME=="file1.txt" {A[substr($1, 1, length($1)-1)]=substr($1, 1, length($1)-1)} FILENAME=="file2.txt"{if(A[$1$2$3$4$5]){print}}' file1.txt file2.txt > matched.txt

现在,我还有一个问题:

如果 file1.txt 的条目(最后一个字符除外)与 file2.txt 的前 5 列匹配,则它必须检查 file1.txt 的最后一个字符(即 1 或 2)。如果最后一个数字/字符是 2,那么它必须在 file2.txt 中搜索 2 个相同的条目(前 5 列),其中第 6 列必须为第一个条目的“F”和第二个条目的“B”。 例如: 文件1.txt

2017042100000000010000000943678177700000000819900000026572

这里最后一位是 2,那么我们必须在 file2.txt 中找到 2 个条目

file2.txt

20170421,0000000001,00000009436781777,000000008199,0000002657,F,3,img_F1_1.tiff
20170421,0000000001,00000009436781777,000000008199,0000002657,B,3,img_F1_1.tiff

同时包含条目“F”和“B”。

如果我们发现 少于 2 个 条目,那么我们必须将丢失的条目存储到文件中,比如missing.txt。我的命令适用于 2 个条目或 0 个条目,但只有一个条目不起作用。

预期输出:

缺少.txt

2017042100000000010000000943678177700000010000000000026622 'B'
2017042100000000010000000943678177700000012855000000026632 'F'
2017042100000000010000000943678177700000012855000000026632 'B'

【问题讨论】:

    标签: shell file awk scripting


    【解决方案1】:

    只解决另一个问题

    $ cat program.awk
    BEGIN { FS="," }                      
    NR==FNR {                              # file1
        if(substr($0,length($0),1)==2) {   # process only records ending in 2
            a[$0 " B"]                     # create B and ...
            a[$0 " F"]                     # ... F entries to a hash
        }
        next
    }
    {                                      # file2
        delete a[$1 $2 $3 $4 $5 2 " " $6]  # delete the ones we meet (* below)
    }
    END {                                  # in the end
        for(i in a)                        # the leftovers (in no order particular)
            print i                        # shall be outputed
    }
    

    * 样本数据使得file2 中的每条记录都将从a 哈希中删除,而不仅仅是具有B F 记录的记录。

    运行它:

    $ awk -f program.awk file1 file2
    2017042100000000010000000943678177700000010000000000026622 B
    2017042100000000010000000943678177700000012855000000026632 B
    2017042100000000010000000943678177700000012855000000026632 F
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-22
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多