Using awk within a for loop

Hello, I currently have managed to get an awk function working inside a for loop that allows me to combine two files based on their headings but what I have not been able to do is print the output to files with variable names.

awk '
        NR==FNR {a[i++]=$0; next} 
        /^>/ {$0 = $0" "a[j++];}
        {print }
        ' "${file%%.txt*}" PRD.good."${sample##*/}"_relabeled 

What I would like to do print the output to a new file with the same string as BITS_PRD.good."${sample##*/}"_relabeled but is this possible to do with awk and print or would it be possible with print? Let me know if I can be more clear about anything! Thank you :slight_smile:

Hello Allie_gastrator,

AFAIU your requirement is to take above awk 's output into a file, you could do so with a simple re-direction, try as follows and let me know how it goes then(Not tested though).

awk '
        NR==FNR {a[i++]=$0; next} 
        /^>/ {$0 = $0" "a[j++];}
        {print }
        ' "${file%%.txt*}" PRD.good."${sample##*/}"_relabeled > BITS_PRD.good."${sample##*/}"_relabeled

If you have some other requirement(which is nt matching this) then please mention details more clearly with expected output too.

Thanks,
R. Singh

1 Like

I tried something similar earlier, I'm not sure why it wasn't working then but this worked too easily!! LOL thannk you!