shell --array in while loop
1
0
Entering edit mode
3.1 years ago
82101202089 ▴ 10

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 • 1.2k views
ADD COMMENT
0
Entering edit mode

cat -n ids.txt should do it? See below for more options:

ADD REPLY
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode

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

ADD REPLY
0
Entering edit mode
3.1 years ago
Sam ★ 4.8k

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

ADD COMMENT

Login before adding your answer.

Traffic: 3086 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6