I am trying to create an array from a file. In the troubleshooting process, I hope to print out all elements of the array to convince myself I'm doing it correctly. However, when I use a hard coded array vs an array constructed from a file, I get different results when trying to output all the array elements.
When I hard code an array like this:
matches=("SviPEND18C_0002" "SviPEND18C_0006")
To get output, I use:
echo "${matches[@]}"
which gives me
SviPEND18C_0002 SviPEND18C_0006
And when I use:
printf '%s\n' "${matches[@]}"
I get:
SviPEND18C_0002
SviPEND18C_0006
All that makes sense to me. However, when I try to make an array from a file and get the output, I don't understand the results. I have a text file (L0316_F.txt) with a single column of sample names, like in the matches array above, but with 50 total. Here's how I put them into an array
mapfile -t IDfemales < <(awk '{print $1}' ../L0316_F.txt)
Now when I use echo as above:
echo "${IDfemales[@]}"
I get the last element of the array only. I was expecting all 50 elements on a single line.
echo "${IDfemales}"
I get the first element of the array only.
However, when I use printf as above:
printf '%s\n' "${IDfemales[@]}"
I get all 50 elements, 1 element per line.
I have already looked at https://stackoverflow.com/questions/41150814/how-to-echo-all-values-from-array-in-bash and I don't get the same results they do.
I would love an explanation. Eventually, I plan to match the elements in the array to file names, which isn't working for me right now, and I assume it's because I don't understand how the array is stored.
echo
. Remove dos line endings. Seedos2unix
andcat -v
andhexdump -C
. Check the output ofdeclare -p IDfemales
awk '{sub(/\r$/, "", $1); print $1}
. You do understand how the array is stored, it's just this hidden carriage return that makes theecho
output confusing. Do this: inspect the array withdeclare -p IDfemales
declare -p IDfemales
gives me ")'49]="SviPEND18C_0149[0]="SviPEND18C_0002. I have no idea what this means. SviPEND18C_0149 is the last element in the array and SviPEND18C_0002 is the first element.I use echo I don't get all the text on 1 line
You do, but the single line is overwritten over and over again, and what is visible on the end is only the last line. Checkcat -v ../L0316_F.txt
of your file. Run your file throughdos2unix
or through that awk command.