Read numbers from control file and Extract matching line numbers from the data file

awksedtext processing

I have a control file – cntl.txt

2
3
5

Data file – data.txt

red
blue
yellow
green
violet
orange

I need to reading the lines matching from control file, here the output expected is:

blue
yellow 
violet

Best Answer

Example of a very inefficient solution:

for i in $(<control.txt); do awk -v c=$i 'NR~c{ print $0 }' data.txt; done;

I report also a good solution I learned tonight:

awk 'FNR==NR{ z[$0]++;next }; FNR in z' control.txt data.txt
Related Question