I have generated a tab delimited file using samtools
and awk
. I am attempting to populate an associative array with the tab delimited file. The keys and values of the associative array will then be used in a function for downstream analysis.
When attempting to populate the associative array using the code the file lines are read into $queryid
and the tab is converted to a space. I have tried running the code with IFS=
and IFS="t"
in addition to what is shown below.
samtools view $1 "NA" | awk 'BEGIN { OFS = "t" } ; { print $1, $4 }' > "$3/$1_ReadStarts.txt"
declare -A Readstart
while IFS= read queryid startpos; do
echo $queryid >> "$3/test.txt"#this line was added for troubleshooting
Readstart[$queryid]=$startpos
done < "$3/$1_ReadStarts.txt"
A portion of the input file (ReadStarts.txt
) generated from samtools
and awk
is shown below. An portion of $queryid
(shown in test.txt
) which should be the keys or the array is shown below. It contains the second column from ReadStarts.txt
which should be stored in $startpos
.
ReadStarts.txt
NB501950:166:HVN2GBGXB:3:21607:13181:3898 13397
NB501950:166:HVN2GBGXB:3:23607:24238:3455 16224
NB501950:166:HVN2GBGXB:3:23402:13620:6968 18402test.txt
NB501950:166:HVN2GBGXB:3:21607:13181:3898 13397
NB501950:166:HVN2GBGXB:3:23607:24238:3455 16224
NB501950:166:HVN2GBGXB:3:23402:13620:6968 18402
1
None of your IFS
settings are correct. IFS=
is the way to unset the variable, not set it equal to a space. In fact, you don’t need to set IFS
at all, since it defaults to spaces, tabs, and newlines.
declare -A Readstart
while read -r queryid startpos; do
echo "$queryid"
echo "$startpos"
Readstart[$queryid]="$startpos"
done < "stackOverflow.txt"
worked for me with the sample file you provided.
If you later need to know how to set IFS
:
- newline:
IFS=$'n'
- tab:
IFS=$'t'
- space:
IFS=' '
1