![如果满足条件,如何打印某些字段](https://linux22.com/image/55460/%E5%A6%82%E6%9E%9C%E6%BB%A1%E8%B6%B3%E6%9D%A1%E4%BB%B6%EF%BC%8C%E5%A6%82%E4%BD%95%E6%89%93%E5%8D%B0%E6%9F%90%E4%BA%9B%E5%AD%97%E6%AE%B5.png)
我有一个很大的输入文件,如下所示。如果“PRIMER_LEFT_NUM_RETURNED”不等于0,则打印相应的SEQUENCE_ID、SEQUENCE_TEMPLATE、PRIMER_LEFT_NUM_RETURNED、PRIMER_RIGHT_NUM_RETURNED、PRIMER_INTERNAL_NUM_RETURNED、PRIMER_PAIR_NUM_RETURNED、PRIMER_PAIR_0_PENALTY以及其中包含“_0”的所有字段。
输入文件:
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
这将在 后添加一个额外的空行=
,如果需要,您可以轻松删除它。