条件が満たされた場合に特定のフィールドを印刷する方法

条件が満たされた場合に特定のフィールドを印刷する方法

以下のような大きな入力ファイルがあります。 「PRIMER_LEFT_NUM_RETURNED」がゼロでない場合は、対応するSEQUENCE_ID、SEQUENCE_TEMPLATE、PRIMER_LEFT_NUM_RETURNED、PRIMER_RIGHT_NUM_RETURNED、PRIMER_INTERNAL_NUM_RETURNED、PRIMER_PAIR_NUM_RETURNE、 。

入力ファイル:

SEQUENCE_ID=Contig1
SEQUENCE_TEMPLATE=AAGTCGCCCCTCCAT
PRIMER_LEFT_NUM_RETURNED=2
PRIMER_RIGHT_NUM_RETURNED=2
PRIMER_INTERNAL_NUM_RETURNED=0
PRIMER_PAIR_NUM_RETURNED=5
PRIMER_PAIR_0_PENALTY=7.398828
PRIMER_LEFT_0_SEQ=AAGTCGCCCC
PRIMER_RIGHT_0_SEQ=aaaagaca
PRIMER_LEFT_0=0,20
PRIMER_RIGHT_0=69,20
PRIMER_LEFT_0_END_STABILITY=2
PRIMER_RIGHT_0_END_STABILITY=3
PRIMER_PAIR_0_PRODUCT_SIZE=70
PRIMER_PAIR_1_PENALTY=7.78
PRIMER_LEFT_1_PENALTY=1
PRIMER_RIGHT_1_PENALTY=6.7
PRIMER_LEFT_1_SEQ=AAGTCGCCCCTCCA
PRIMER_RIGHT_1_SEQ=aaaagacagagaG
PRIMER_LEFT_1=0,19
PRIMER_RIGHT_1=69,25
PRIMER_LEFT_1_END_STABILITY=3
PRIMER_RIGHT_1_END_STABILITY=3
PRIMER_PAIR_1_PRODUCT_SIZE=70
=
SEQUENCE_ID=Contig31
SEQUENCE_TEMPLATE=ACCCCTTTTT
PRIMER_LEFT_NUM_RETURNED=0
PRIMER_RIGHT_NUM_RETURNED=0
=
SEQUENCE_ID=Contig22
SEQUENCE_TEMPLATE=CCGTCGCCCC
PRIMER_LEFT_NUM_RETURNED=3
PRIMER_RIGHT_NUM_RETURNED=3
PRIMER_INTERNAL_NUM_RETURNED=0
PRIMER_PAIR_NUM_RETURNED=5
PRIMER_PAIR_0_PENALTY=7
PRIMER_LEFT_0_SEQ=AAGTCGCC
PRIMER_RIGHT_0_SEQ=agaGAGTA
PRIMER_LEFT_0=0,20
PRIMER_RIGHT_0=69,20
PRIMER_LEFT_0_END_STABILITY=2
PRIMER_RIGHT_0_END_STABILITY=3
PRIMER_PAIR_0_PRODUCT_SIZE=70
PRIMER_PAIR_1_PENALTY=7
PRIMER_LEFT_1_PENALTY=1
PRIMER_RIGHT_1_PENALTY=6
PRIMER_LEFT_1_SEQ=AAGTCGC
PRIMER_RIGHT_1_SEQ=aaaag
PRIMER_LEFT_1=0,19
PRIMER_RIGHT_1=69,25
PRIMER_LEFT_1_END_STABILITY=3
PRIMER_RIGHT_1_END_STABILITY=3
PRIMER_PAIR_1_PRODUCT_SIZE=73
PRIMER_PAIR_2_PENALTY=7
PRIMER_LEFT_2_PENALTY=1
PRIMER_RIGHT_2_PENALTY=6
PRIMER_LEFT_2_SEQ=AAGTCGC
PRIMER_RIGHT_2_SEQ=aaaag
PRIMER_LEFT_2=0,19
PRIMER_RIGHT_2=69,25
PRIMER_LEFT_2_END_STABILITY=3
PRIMER_RIGHT_2_END_STABILITY=3
PRIMER_PAIR_2_PRODUCT_SIZE=75
=

予想出力:

SEQUENCE_ID=Contig1
SEQUENCE_TEMPLATE=AAGTCGCCCCTCCAT
PRIMER_LEFT_NUM_RETURNED=2
PRIMER_RIGHT_NUM_RETURNED=2
PRIMER_INTERNAL_NUM_RETURNED=0
PRIMER_PAIR_NUM_RETURNED=5
PRIMER_PAIR_0_PENALTY=7.398828
PRIMER_LEFT_0_SEQ=AAGTCGCCCC
PRIMER_RIGHT_0_SEQ=aaaagaca
PRIMER_LEFT_0=0,20
PRIMER_RIGHT_0=69,20
PRIMER_LEFT_0_END_STABILITY=2
PRIMER_RIGHT_0_END_STABILITY=3
PRIMER_PAIR_0_PRODUCT_SIZE=70
=
SEQUENCE_ID=Contig22
SEQUENCE_TEMPLATE=CCGTCGCCCC
PRIMER_LEFT_NUM_RETURNED=3
PRIMER_RIGHT_NUM_RETURNED=3
PRIMER_INTERNAL_NUM_RETURNED=0
PRIMER_PAIR_NUM_RETURNED=5
PRIMER_PAIR_0_PENALTY=7
PRIMER_LEFT_0_SEQ=AAGTCGCC
PRIMER_RIGHT_0_SEQ=agaGAGTA
PRIMER_LEFT_0=0,20
PRIMER_RIGHT_0=69,20
PRIMER_LEFT_0_END_STABILITY=2
PRIMER_RIGHT_0_END_STABILITY=3
PRIMER_PAIR_0_PRODUCT_SIZE=70
=

私はawkを使って解決しようとしましたが、行と列の形ではないので解決できませんでした。この問題の解決にご協力いただきありがとうございます。ありがとうございます。

答え1

入力データは段落中心なので、一行ずつ読みずに段落別に読みましょう。

awk -v RS="\n=\n" '
    /PRIMER_LEFT_NUM_RETURNED=[^0]/ {
        n = split($0, lines, /\n/)
        for (i=1; i<=n; i++) {
            if (lines[i] ~ /^(SEQUENCE_ID|SEQUENCE_TEMPLATE|PRIMER_LEFT_NUM_RETURNED|PRIMER_RIGHT_NUM_RETURNED|PRIMER_INTERNAL_NUM_RETURNED|PRIMER_PAIR_NUM_RETURNED|[^=]+_0[^=]*)=/)
                print lines[i]
        }
        print "="
    }
' input.file

またはPerlと同等のもの(より読みやすい「拡張」正規表現を許可する)

perl -0777 -ne '
    BEGIN {
        $wanted = qr{ 
            ^                  # at the beginning of the string
            (?: SEQUENCE_ID    # match one of these words
               | SEQUENCE_TEMPLATE 
               | PRIMER_LEFT_NUM_RETURNED 
               | PRIMER_RIGHT_NUM_RETURNED 
               | PRIMER_INTERNAL_NUM_RETURNED 
               | PRIMER_PAIR_NUM_RETURNED 
               | [^=]+_0[^=]*
            )
            =                  # followed by an equal sign
        }x
    }

    for (split /^=$/m) {
        if (/PRIMER_LEFT_NUM_RETURNED=[^0]/) {
            print join("\n", grep {$_ =~ $wanted} split /\n/), "\n=\n";
        }
    }

    # or, as a single command:
    #
    # print 
    #     map {join("\n", grep {$_ =~ $wanted} split /\n/) . "\n=\n"}
    #     grep {/PRIMER_LEFT_NUM_RETURNED=[^0]/}
    #     split /^=$/m

' input.file

答え2

レコード区切り記号をに設定できます\n=\n。これは改行、a =、そして改行です。必要に応じて awk を使用できます。

$ awk -v RS='\n=\n' -v OFS="\n" '!/PRIMER_LEFT_NUM_RETURNED=0/{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,RS}' file
SEQUENCE_ID=Contig1
SEQUENCE_TEMPLATE=AAGTCGCCCCTCCAT
PRIMER_LEFT_NUM_RETURNED=2
PRIMER_RIGHT_NUM_RETURNED=2
PRIMER_INTERNAL_NUM_RETURNED=0
PRIMER_PAIR_NUM_RETURNED=5
PRIMER_PAIR_0_PENALTY=7.398828
PRIMER_LEFT_0_SEQ=AAGTCGCCCC
PRIMER_RIGHT_0_SEQ=aaaagaca
PRIMER_LEFT_0=0,20
PRIMER_RIGHT_0=69,20
PRIMER_LEFT_0_END_STABILITY=2
PRIMER_RIGHT_0_END_STABILITY=3
PRIMER_PAIR_0_PRODUCT_SIZE=70

=

SEQUENCE_ID=Contig22
SEQUENCE_TEMPLATE=CCGTCGCCCC
PRIMER_LEFT_NUM_RETURNED=3
PRIMER_RIGHT_NUM_RETURNED=3
PRIMER_INTERNAL_NUM_RETURNED=0
PRIMER_PAIR_NUM_RETURNED=5
PRIMER_PAIR_0_PENALTY=7
PRIMER_LEFT_0_SEQ=AAGTCGCC
PRIMER_RIGHT_0_SEQ=agaGAGTA
PRIMER_LEFT_0=0,20
PRIMER_RIGHT_0=69,20
PRIMER_LEFT_0_END_STABILITY=2
PRIMER_RIGHT_0_END_STABILITY=3
PRIMER_PAIR_0_PRODUCT_SIZE=70

=

しかしこれは。想定されるフィールドは_0常に同じです。そうでない場合は、次のことができます。

$ awk -v RS='\n=\n' -v OFS="\n\n" '!/PRIMER_LEFT_NUM_RETURNED=0/{printf "%s\n\n", $0}' file | 
   grep -P 'SEQUENCE_ID|SEQUENCE_TEMPLATE|PRIMER_LEFT_NUM_RETURNED|PRIMER_RIGHT_NUM_RETURNED|PRIMER_INTERNAL_NUM_RETURNED|PRIMER_PAIR_NUM_RETURNED|_0'

答え3

直接方法:

awk '$3 !~ "=0"{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,RS}' RS='=\n' FS='\n' OFS='\n' file

これにより、後ろに空白行が追加され、=必要に応じて簡単に削除できます。

関連情報