I am trying to extract specific columns from a file using gawk command, but I need to do this several times.

gawk -F" " '{print $2, $7, $8, $9}' acr30R_vs_acr30NR_1.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_1.txt

gawk -F" " '{print $2, $7, $8, $9}' acr30R_vs_acr30NR_2.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_2.txt

gawk -F" " '{print $2, $7, $8, $9}' acr30R_vs_acr30NR_3.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_3.txt

The above code is to extract and print from 3 files (with suffix 1 , 2 and 3 as highlighted). I want to be able to do this for upto 50


You can do this with a shell loop:

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

You can replace the 1 and 3 values as appropriate in the first line. Note ${i} in the second line (in two places): these are replaced by each value in the sequence, in turn.

  • Hi Stephen, Thank you for answering this so quickly, and it is working as expected. Regards - Sam – Sam Jan 5 '17 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.