Reputation: 35
I'm having trouble ignoring the commas within a bash variable so that the csv file doesn't split up the variable into different columns. If the variable were "TACGTAT,TACG", I would want that as a single column instead of two different columns.
Here is my full script:
for filename in "$1"/*.vcf; do
bcftools query -f '%POS %REF %ALT\n' "$filename" > temp_reads.txt
echo "Sick Read!: "$(cat temp_reads.txt)""
echo ""$(basename "$filename")","$(cat temp_reads.txt)"" >> output.csv
done
And I specifically want everything in the "$(cat temp_reads.txt)" expansion to be included as a single column in the csv file in case there happened to be a comma in there.
Thanks!
Upvotes: 0
Views: 1331
Reputation: 189387
The wrangling of temporary files is completely unnecessary anyway.
for filename in "$1"/*.vcf; do
bcftools query -f '%POS %REF %ALT\n' "$filename" |
sed "s/^/$(basename "$filename"),/"
done >output.csv
Generally speaking, you cannot nest double quotes: ""foo
is just an unquoted foo
with an empty quoted string to its left (which of course disappears entirely by the time the shell is done parsing this expression).
Notice also how moving the redirection after the done
improves legibility and efficiency. Because you only redirect once, you can write instead of append (assuming you don't need to append for other reasons, of course) and you don't open, seek to the end of the file, write, and close every time through the loop, so you save a fair bit on the I/O overhead.
Upvotes: 1
Reputation: 20002
Testdata:
echo 'a,b and "c".' > temp_reads.txt
filename="What a lovely name"
Incorrect solution ignoring double quotes in the file
printf '"%s","%s"\n' "${filename}" "$(cat temp_reads.txt)"
Escaping each double quote with a second one in file
sed 's/"/""/g' temp_reads.txt
Combined
printf '"%s","%s"\n' "${filename}" "$(sed 's/"/""/g' temp_reads.txt)"
Upvotes: 0