Generating a list of Fibonacci numbers is tricky since they get big quickly, so I just found a . I removed the numbering with cut -f2. Then run your file and this list through grep.
cat test.txt
1 2 3 4 5 6 7 8 9 10 11 12 13
cat fib.txt
0 1 1 2 3 5 8 ...
grep -w -F -f fib.txt test.txt
1 2 3 4 5 6 7 8 9 10 11 12 13
Add -o to only print the matched words, one per line.
grep -o -w -F -f fib.txt test.txt
1 2 3 5 8 13
Don't write code if you don't need to, unless this is an exercise to learn a particular language.
EDIT: I see, you want the nth words of a text, for only n that are in the Fibonacci sequence.
Generating a list of Fibonacci numbers is tricky since they get big quickly, so I just found a [pre-made list of the first 500](https://web.archive.org/web/20120503110830/http://aux.planetmath.org/files/objects/7680/fib.txt). I removed the numbering with cut -f2. Then run your file and this list through grep.
cat test.txt
> 1 2 3 4 5 6 7 8 9 10 11 12 13
cat fib.txt
> 0
> 1
> 1
> 2
> 3
> 5
> 8
> ...
grep -w -F -f fib.txt test.txt
> **1 2 3** 4 **5** 6 7 **8** 9 10 11 12 **13**
Add -o to only print the matched words, one per line.
grep -o -w -F -f fib.txt test.txt
> 1
> 2
> 3
> 5
> 8
> 13
Don't write code if you don't need to, unless this is an exercise to learn a particular language.
EDIT: I see, you want the nth words of a text, for only n that are in the Fibonacci sequence.
(post is archived)