Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

I have list of data array which I want to look the values by its index (id) and store to the index id from the data array. I can do it in spreadsheet VALUE LOOKUP function, but I still have lots of data to do this and I don't think spreadsheet function will be fast for huge data sets. I want you help me to let me know the script that can do it faster. My data array, index id and proposed out put is as follows.

data array

Id  Value 

1    10
2    20
3    30
4    40
6    60
8    80
10   100

index

Id 

1
2
3
4
5
6
7
8
9
10
11
12
13

Output will be

ID    Value
1  10
2  20
3  30
4  40
5  NULL
6  60
7  NULL
8  80
9  NULL 
10 100
11  NULL
12  NULL
13  NULL

Thank you for helping again.

share|improve this question

marked as duplicate by Gilles, slm, jasonwryan, Mat, Anthon Aug 29 '13 at 4:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
this question is already solve unix.stackexchange.com/questions/88550/vlookup-function-in-unix –  Rahul Patil Aug 28 '13 at 15:22
    
see output paste.ubuntu.com/6036949 –  Rahul Patil Aug 28 '13 at 15:22
    
@RahulPatil,Thank you very much! It helps a lot and work perfect –  Abraham Aug 28 '13 at 19:08
    
@RahulPatil When you see a duplicate question, please flag it, select “it doesn't belong here, or it is a duplicate…”, then “duplicate of”, and enter the link to the duplicate. –  Gilles Aug 28 '13 at 21:44

1 Answer 1

Try a nice mixture of awk and redis (an extremely quick open source NoSQL key-value store. See http://redis.io for details).

Use awk to parse your 2 files ("data_array.txt" and "index.txt") and to generate your redis commands.

Pipe the result of the 2 awk scripts into bash to execute them. That's it :-)

Step by step:

Generate your redis "SET" statements by parsing the data file starting with the third line like this:

awk '{if (FNR > 2) \
print "redis-cli SET VALUE:" $1 " \"" $2"\""}' data_array.txt
redis-cli SET VALUE:1 "10"
redis-cli SET VALUE:2 "20"
redis-cli SET VALUE:3 "30"
redis-cli SET VALUE:4 "40"
redis-cli SET VALUE:6 "60"
redis-cli SET VALUE:8 "80"
redis-cli SET VALUE:10 "100"

Pipe your redis "SET" statements into bash to execute them:

awk '{if (FNR > 2) \
print "redis-cli SET VALUE:" $1 " \"" $2"\""}' data_array.txt | bash
OK
OK
OK
OK
OK
OK
OK

Generate your redis "GET" statements by parsing the index file starting with the third line like this:

awk '{if (FNR > 2) print "printf \"" $1 " \" && redis-cli GET VALUE:" $1}' index.txt
echo 1 && redis-cli GET VALUE:1
echo 2 && redis-cli GET VALUE:2
echo 3 && redis-cli GET VALUE:3
echo 4 && redis-cli GET VALUE:4
echo 5 && redis-cli GET VALUE:5
echo 6 && redis-cli GET VALUE:6
echo 7 && redis-cli GET VALUE:7
echo 8 && redis-cli GET VALUE:8
echo 9 && redis-cli GET VALUE:9
echo 10 && redis-cli GET VALUE:10
echo 11 && redis-cli GET VALUE:11
echo 12 && redis-cli GET VALUE:12
echo 13 && redis-cli GET VALUE:13

Pipe your redis "GET" statements into bash to execute them:

awk '{if (FNR > 2) \
print "printf \"" $1 " \" && redis-cli GET VALUE:" $1}' index.txt |\
bash
1 "10"
2 "20"
3 "30"
4 "40"
5 (nil)
6 "60"
7 (nil)
8 "80"
9 (nil)
10 "100"
11 (nil)
12 (nil)
13 (nil)

Enjoy :-)

bernie

@Rahul Patil
@Gilles

I posted my answer here on purpose. Abraham's example data are nicely transparent so hopefully my answer is quickly to understand. The data in the vlookup function in unix example looked confusing to me...

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.