1 year ago

#378246

test-img

zoëshell

Troubleshoot for loop with arithmetic in shell

I'm trying to write a loop that pulls sequencing metrics from column 2 of a txt file (ending in full_results.txt) and writes everything into a combined tsv (ideally with headers as well, but I haven't been able to do that).

The below makes a tsv with all of the columns I want--but stopped looping. The loop was working before I added the last two columns, so I'm not sure if adding the fields with arithmetic changed anything. Let me know if there is a cleaner way to write this! Thanks for your input.

{
for i in *full_results.txt;
do
[ -d "$i" ] && continue
[ -s "$1" ] && continue
                total=$(awk ' $1=="NORMALIZED_READ_COUNT" { print $2 } ' $i)
                trimmed=$(awk ' $1=="RAW_FRAGMENT_TOTAL" { print $2 } ' $i)
                aligned=$(awk ' $1=="RAW_FRAGMENT_FILTERED" { print $2 } ' $i)
                molbin=$(awk ' $1=="AVERAGE_UNIQUE_DNA_READS_PER_GSP2" { print $2 } ' $i)
                startsite=$(awk ' $1=="AVERAGE_UNIQUE_DNA_START_SITES_PER_GSP2" { print $2 } ' $i)
                dedup=$(awk ' $1=="ON_TARGET_DEDUPLICATION_RATIO" { print $2 } ' $i)
                printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" "$i" $total $trimmed $aligned $molbin $startsite $dedup $((total - trimmed)) "$(( ( (total - trimmed) * 100) / total))"
done;
} > sequencing_metrics.tsv

Output:

NR02_31_S31_merged_R1_001_full_results.txt 7095319 6207119 6206544 1224.43 391.65 2.74:1 888200 12

Intended output: the same as above but looped for all files in the folder

loops

shell

csv

printf

0 Answers

Your Answer

Accepted video resources