0

I have a long bash script in which I indicate a sample/patient # using the term "patient_number=$1" such that I can run the script as:

sh myscript.sh 551
sh myscript.sh 1034

(i.e. for different patients)

The script works flawlessly until I get to the final step, just sorting some things with awk:

awk '{ OFS = "\t" ; if ($5 + 6 >= 5) print > "Cflo"$patient_number"_5Xcovered_withcoverage.txt" }' /path/to/my/original/filefor"$patientnumber".txt

The command itself it works when I run it manually. But here, it begins to create infinite number of files, replacing "$patient_number" with a string of numbers and question marks like so:

Cflo9?9945?9945?0?0?9_5Xcovered_withcoverage.txt
Cflo9?9948?9948?0?0?6_5Xcovered_withcoverage.txt
Cflo9?9951?9951?0?0?6_5Xcovered_withcoverage.txt
Cflo9?9952?9952?0?0?7_5Xcovered_withcoverage.txt
Cflo9?9956?9956?0?0?6_5Xcovered_withcoverage.txt
Cflo9?9957?9957?0?0?6_5Xcovered_withcoverage.txt
Cflo9?9961?9961?0?0?5_5Xcovered_withcoverage.txt
Cflo9?9968?9968?0?0?5_5Xcovered_withcoverage.txt
Cflo9?9972?9972?0?0?4_5Xcovered_withcoverage.txt
Cflo9?9976?9976?0?0?5_5Xcovered_withcoverage.txt
Cflo9?9977?9977?0?0?5_5Xcovered_withcoverage.txt
Cflo9?9979?9979?0?0?2_5Xcovered_withcoverage.txt
Cflo9?9980?9980?0?0?2_5Xcovered_withcoverage.txt
Cflo9?9983?9983?0?0?4_5Xcovered_withcoverage.txt
Cflo9?9984?9984?0?0?2_5Xcovered_withcoverage.txt
Cflo9?99?99?0?0?8_5Xcovered_withcoverage.txt

I let it run almost to 1 million files before I stopped it. Does anyone know what may be causing this?

Thanks!

DanS
  • 47
  • 6
  • 2
    Awk doesn't have access to shell variables. `patient_number` defauts to 0 so you end up referring to `$0` i.e. the entire input line. – tripleee Jun 16 '18 at 20:23

1 Answers1

0

Actually, I found that just rearranging this fixes it. Must be a problem with the double quotation marks:

from this:

awk '{ OFS = "\t" ; if ($5 + 6 >= 5) print > "Cflo"$patient_number"_5Xcovered_withcoverage.txt" }' /path/to/my/original/filefor"$patientnumber".txt

to this:

awk '{ OFS = "\t" ; if ($5 + 6 >= 5) print $0 }' /path/to/my/original/filefor"$patientnumber".txt > Cflo"$patient_number"_5Xcovered_withcoverage.txt
DanS
  • 47
  • 6
  • 1
    FYI `awk '{ OFS = "\t" ; if ($5 + 6 >= 5) print $0 }'` = `awk '$5 >= -1'`. And no, the problem has nothing to do with the double quotation marks. – Ed Morton Jun 17 '18 at 13:18