如何循环运行 gawk 命令

如何循环运行 gawk 命令

我正在尝试使用 gawk 命令从文件中提取特定列,但我需要执行多次。

gawk -F" " '{打印 $2, $7, $8, $9}' acr30R_vs_acr30NR_1.assoc.logistic>>/mnt/jw01-aruk home01/projects/jia_mtx_gwas_2016/common_files/output/impulated_dataset/all_50_mi_datasets/acr30R_vs_acr30NR_combined_coefficients/acr30nr_r_logistic_1。TXT

gawk -F" " '{打印 $2, $7, $8, $9}' acr30R_vs_acr30NR_2.assoc.logistic>>/mnt/jw01-aruk-home01/projects/jia_mtx_gwas_2016/common_files/output/impulated_dataset/all_50_mi_datasets/acr30R_vs_acr30NR_combined_coefficients/acr30nr_r_logistic_2。TXT

gawk -F" " '{打印 $2, $7, $8, $9}' acr30R_vs_acr30NR_3.assoc.logistic>>/mnt/jw01-aruk-home01/projects/jia_mtx_gwas_2016/common_files/output/impulated_dataset/all_50_mi_datasets/acr30R_vs_acr30NR_combined_coefficients/acr30nr_r_logistic_3。TXT

上面的代码是从 3 个文件中提取并打印(后缀 1 、 2 和 3 突出显示)。我希望能够最多 50 次执行此操作

答案1

您可以使用 shell 循环来完成此操作:

for i in $(seq 1 3); do
    gawk -F" " '{print $2, $7, $8, $9}' acr30R_vs_acr30NR_${i}.assoc.logistic >>/mnt/jw01-aruk-home01/projects/jia_mtx_gwas_2016/common_files/output/imputed_dataset/all_50_mi_datasets/acr30R_vs_acr30NR_combined_coefficients/acr30nr_r_logistic_${i}.txt
done

您可以根据需要替换第一行中的1和值。3注意${i}第二行(在两个地方):这些依次被序列中的每个值替换。

相关内容