bash - 从多个文件中获取字符串并复制到新文件并将文件名打印到 bash 的第二列

我有多个包含此信息的文件:

sP12345.txt

COMMENT     Method: conceptual translation.
FEATURES             Location/Qualifiers
     source          1..3024
                     /organism="H"
                     /isolate="sP12345"
                     /isolation_source="blood"
                     /host="Homo sapiens"
                     /db_xref="taxon:11103"
                     /collection_date="31-Mar-2014"
                     /note="genotype: 3"

sP4567.txt

COMMENT     Method: conceptual translation.
FEATURES             Location/Qualifiers
     source          1..3024
                     /organism="H"
                     /isolate="sP4567"
                     /isolation_source="blood"
                     /host="Homo sapiens"
                     /db_xref="taxon:11103"
                     /collection_date="31-Mar-2014"
                     /note="genotype: 2"

现在我想获取 /note="genotype: 3" 并仅复制 genotype: 之后的数字,将其复制到新的文本文件并打印文件名已作为第 2 列。

预期输出:

3  sP12345
2  sP4567

我试过这段代码:但它只打印第一列而不是文件名:

awk -F'note="genotype: ' -v OFS='\t' 'FNR==1{++c} NF>1{print $2, c}' *.txt > output_file.txt

最佳答案

您可以使用:

awk '/\/note="genotype: /{gsub(/^.* |"$/, ""); f=FILENAME; sub(/\.[^.]+$/, "", f); print $0 "\t" f}' sP*.txt

3   sP12345
2   sP4567

https://stackoverflow.com/questions/71338798/

相关文章:

ruby-on-rails - 安装带有 native 扩展 Gem::Ext::BuildErro

swift - RealityKit——在检测到的平面上可视化网格

R - 如何将多个 bool 列(不知道有多少)组合成一列

r - 在列 R 中包含列表元素

r - 一种扩展R向量的有效方法

kubernetes - 在 WSL2 中安装 Minikube 集群时无法访问已部署的服务

r - 检查列表中的值是否存在于多列 R data.table 中

flutter - 图标聚集在带有 float 按钮的 flutter 底部导航栏中

powershell - 仅添加真实 MAC 地址

python - 独特的骰子组合