fish out lines according to line number

Dear all,

I've a situation here, I have a big file which contains about 20Million line as shown as below

I want to fish out some lines according to the line position of the big file, for example here line number 3,4,7 ...
so the expected output will be like this

currently I manage to fish out the sequence by creating scripting using sed command

However i have millions of lines to be fished out. Using sed command as above takes me days to get the job done. This is because the sed will read from start of the line until end and it continue for each search.

Is there any other way to make things done faster.

Thanks in advance

kamal

Assuming your numbers are in the file "line.numbers" like so
3
4
7

perl -lne '                                                                                                                                               
BEGIN {
  open $fnums, "<" , shift or die $!;
  @nums =  <$fnums>;
  chomp @nums;
}
$. == $nums[0] && print && shift @nums;
' line.numbers INPUTFILE
1 Like

PERFECT!!!!

Thanks a lot Yuzu. You are really great. Your script takes only minutes to finish the job.

Hope I can be as good as this.

Thanks again

Could this help you?

exp="";for i in 3 4 7; do exp=$exp$i"p;"; done;sed -n "$exp" inputfile