Awk conditional filter one file based on another (or other solutions)

前端 未结 1 1921
夕颜
夕颜 2021-01-28 12:05

Programming beginner here needs some help modifying an AWK script to make it conditional. Alternative non-awk solutions are also very welcome.

NOTE Main

相关标签:
1条回答
  • 2021-01-28 12:43

    One way.

    awk '
        BEGIN { FS = "\t"; }
    
        ## Save third, fifth and seventh field of first file in arguments (refGene.txt) as the key
        ## to compare later. As value the field to print.
        FNR == NR {
            pair[ $3, $5, $6 ] = $13;
            next;
        }
    
        ## Set the name of the output file.
        FNR == 1 {
            output_file = "";
            split( ARGV[ARGIND], path, /\// );
            for ( i = 1; i < length( path ); i++ ) {
                current_file = ( output_file ? "/" : "" ) path[i];
            }
            output_file = output_file "/ListOfGenes_" path[i];
        }
    
        ## If $1 = $3, $2 = $5 and $3 = $6, print $13 to output file.
        {
            if ( pair[ $1, $2, $3 ] ) {
                print pair[ $1, $2, $3 ] >output_file;
            }
        }
    ' refGene.txt /files/rubal/*.txt
    
    0 讨论(0)
提交回复
热议问题