从 awk 生成的制表符分隔文件中填充关联数组的故障排除
Troubleshooting populating associative array from awk generated tab delimited file
我使用 samtools
和 awk
生成了制表符分隔文件。我试图用制表符分隔文件填充关联数组。然后,关联数组的键和值将用于下游分析的函数中。
当尝试使用代码填充关联数组时,文件行被读入 $queryid
并且制表符被转换为 space。除了下面显示的内容之外,我还尝试了 运行 使用 IFS=\
和 IFS="\t"
的代码。
samtools view "NA" | awk 'BEGIN { OFS = "\t" } ; { print , }' > "/_ReadStarts.txt"
declare -A Readstart
while IFS= read queryid startpos; do
echo $queryid >> "/test.txt"#this line was added for troubleshooting
Readstart[$queryid]=$startpos
done < "/_ReadStarts.txt"
从 samtools
和 awk
生成的输入文件 (ReadStarts.txt
) 的一部分如下所示。 $queryid
的一部分(显示在 test.txt
中)应该是键或数组,如下所示。它包含 ReadStarts.txt
中的第二列,应存储在 $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 18402
test.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
None 的 IFS
设置是正确的。 IFS=
是 取消设置 变量的方法,而不是将其设置为等于 space。事实上,您根本不需要设置 IFS
,因为它默认为 space、制表符和换行符。
declare -A Readstart
while read -r queryid startpos; do
echo "$queryid"
echo "$startpos"
Readstart[$queryid]="$startpos"
done < "Whosebug.txt"
使用您提供的示例文件为我工作。
如果以后需要知道如何设置IFS
:
- 换行符:
IFS=$'\n'
- 选项卡:
IFS=$'\t'
- space:
IFS=' '
我使用 samtools
和 awk
生成了制表符分隔文件。我试图用制表符分隔文件填充关联数组。然后,关联数组的键和值将用于下游分析的函数中。
当尝试使用代码填充关联数组时,文件行被读入 $queryid
并且制表符被转换为 space。除了下面显示的内容之外,我还尝试了 运行 使用 IFS=\
和 IFS="\t"
的代码。
samtools view "NA" | awk 'BEGIN { OFS = "\t" } ; { print , }' > "/_ReadStarts.txt"
declare -A Readstart
while IFS= read queryid startpos; do
echo $queryid >> "/test.txt"#this line was added for troubleshooting
Readstart[$queryid]=$startpos
done < "/_ReadStarts.txt"
从 samtools
和 awk
生成的输入文件 (ReadStarts.txt
) 的一部分如下所示。 $queryid
的一部分(显示在 test.txt
中)应该是键或数组,如下所示。它包含 ReadStarts.txt
中的第二列,应存储在 $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
None 的 IFS
设置是正确的。 IFS=
是 取消设置 变量的方法,而不是将其设置为等于 space。事实上,您根本不需要设置 IFS
,因为它默认为 space、制表符和换行符。
declare -A Readstart
while read -r queryid startpos; do
echo "$queryid"
echo "$startpos"
Readstart[$queryid]="$startpos"
done < "Whosebug.txt"
使用您提供的示例文件为我工作。
如果以后需要知道如何设置IFS
:
- 换行符:
IFS=$'\n'
- 选项卡:
IFS=$'\t'
- space:
IFS=' '