首页 > 解决方案 > 如何从shell脚本中的一行中提取两个部分数值

问题描述

我有多个这种格式的文本文件。我想提取与此模式“通过过滤器和 QC”匹配的行。

文件1:

Before main variant filters, 309 founders and 0 nonfounders present.
0 variants removed due to missing genotype data (--geno).
9302015 variants removed due to minor allele threshold(s)
(--maf/--max-maf/--mac/--max-mac).
7758518 variants and 309 people pass filters and QC.
Calculating allele frequencies... done.

我能够 grep 行,但是当我尝试分配给行变量时它不起作用。

grep  'people pass filters and QC' File1
line="$(echo grep 'people pass filters and QC' File1)"

我是 shell 脚本的新手,如果你能帮我这样做,我将不胜感激。

我想创建一个制表符分隔的文件

"File1" "7758518 variants" "309 people"

标签: linuxbashshell

解决方案


GNU awk

gawk '
  BEGIN { patt = "([[:digit:]]+ variants) .* ([[:digit:]]+ people) pass filters and QC" }
  match($0, patt, m) {printf "\"%s\" \"%s\" \"%s\"\n", FILENAME, m[1], m[2]}
' File1

推荐阅读