linux_vlookup

作者: 天骏 | 来源:发表于2018-11-16 21:20 被阅读0次

    https://stackoverflow.com/questions/32481877/what-is-nr-fnr-in-awk

    Look for keys (first word of line) in file2 that are also in file1.
    Step 1: fill array a with the first words of file 1:

    awk '{a[$1];}' file1
    

    Step 2: Fill array a and ignore file 2 in the same command. For this check the total number of records until now with the number of the current input file.

    awk 'NR==FNR{a[$1]}' file1 file2
    

    Step 3: Ignore actions that might come after } when parsing file 1

    awk 'NR==FNR{a[$1];next}' file1 file2 
    

    Step 4: print key of file2 when found in the array a

    awk 'NR==FNR{a[$1];next} $1 in a{print $1}' file1 file2
    
    pony@z2air:/mnt/d/Biosoft/MCScanX/at$ head at.test
    evm.model.fragScaff_scaffold_15.30 
    evm.model.fragScaff_scaffold_15.66 
    evm.model.fragScaff_scaffold_15.52 
    evm.model.fragScaff_scaffold_15.52 
    evm.model.fragScaff_scaffold_15.52 
    evm.model.fragScaff_scaffold_15.52 
    evm.model.fragScaff_scaffold_15.52 
    evm.model.fragScaff_scaffold_15.139
    evm.model.fragScaff_scaffold_15.81 
    evm.model.fragScaff_scaffold_15.81 
    pony@z2air:/mnt/d/Biosoft/MCScanX/at$ head at.gff
    fc15    evm.model.fragScaff_scaffold_15.18      172819  173259
    fc15    evm.model.fragScaff_scaffold_15.30      314441  318368
    fc15    evm.model.fragScaff_scaffold_15.109     1117575 1118396
    fc15    evm.model.fragScaff_scaffold_15.140     1593997 1594908
    fc15    evm.model.fragScaff_scaffold_15.66      684328  686553
    fc15    evm.model.fragScaff_scaffold_15.52      540989  544116
    fc15    evm.model.fragScaff_scaffold_15.96      1018996 1024259
    fc15    evm.model.fragScaff_scaffold_15.139     1584325 1585666
    fc15    evm.model.fragScaff_scaffold_15.7       46014   46783
    fc15    evm.model.fragScaff_scaffold_15.81      814968  815669
    
    $ awk 'NR==FNR{a[$1];next}$2 in a {print $1}' at.test at.gff >test
    NR:
    FNR:
    a[$1]: evm.model.fragScaff_scaffold_15.18
    $2: fc15    evm.model.fragScaff_scaffold_15.18
    print $1: fc15
    
    

    相关文章

      网友评论

        本文标题:linux_vlookup

        本文链接:https://www.haomeiwen.com/subject/pysvfqtx.html