-1

我想打印第二列中包含两个字符的所有文件名。我怎样才能用 awk 做到这一点?

AA.txt

REM  A   7   35.32  
REM  A   8    47.17                         
REM  B   9    988.62                                
REM  D  10   111.59

BB.txt

REM  A   7    135.32                                           
REM  A   8    647.19                         
REM  B   9    88.62 

抄送.txt

REM  A   7    135.32                                           
REM  A   8    247.17                         
REM  B   9    188.62                                
REM  B  10    111.56   

期望的输出

BB.txt
CC.txt

您的建议将不胜感激!

4

2 回答 2

3
for i in *.txt; do
    if [ $(awk '{ print $2 }' $i | sort -u | wc -l) -eq 2 ]; then
        echo $i
    fi
done
于 2012-09-29T16:17:06.520 回答
2

一种仅使用的方法GNU awk

awk '
    ## A function that prints the file name if only found two different strings
    ## in the second field.
    function check_col_and_print_file(c, i) {
        if ( length(c) == 2 ) {
            printf "%s\n", ARGV[ i ];
        }
    }

    ## At the beginning of each file but the first one, call the function to
    ## check if the file must be printed, and reset the array for the processing
    ## of the next file (the current one because we already are in first line).
    FNR == 1 && NR > 1 { 
        check_col_and_print_file(cols, ARGIND-1);
        delete cols; 
    } 

    ## Save second field as a key of a hash to avoid repeated values.
    {
        cols[ $2 ] = 1;
    }

    ## Same as before but in the "END" block because we finished the processing
    ## of all input files.
    END {
        check_col_and_print_file(cols, ARGIND);
    }
' AA.txt BB.txt CC.txt

这会产生:

BB.txt
CC.txt
于 2012-09-29T17:16:57.827 回答