This is a test version of Biostars. For the public version, visit https://www.biostars.org.
shell --array in while loop

first I have a file which have some gene_IDs: ZM011,ZD012,ZF033,..... Then I want to convert these gene id to different numbers. Like ZM011 -> 1, ZD012 -> 2,.... I try use shell script like this:

   i=1
   cat ids.txt | while read id; do arr[$id]=$i let i++; done
   echo ${#arr[@]}

but the output is 0

I don't know how to do this

shell

Thank you very much!

But, actually, I need to form a correspondence between the gene ID and the number for subsequent interconversion.

For example, I would save the generated numbers in a file to reduce the size of the file, and then pull out one and convert it back to the corresponding ID when needed

That's why I want to save it as an array

save the output of cat -n in a sorted file. At the end use join.

1 answer

Not sure why will this be helpful, as gziping the file might save you more space to start with anyway, but you can do

awk -v it=0 '{for(i = 0; i <=NF; ++i){print $i,it; it++}}' ids.txt > newFile

Log in to answer this question.