perl -e '$cond = join " || ", map { "($_ .. \$. == $_ + 2)" } do { local @ARGV = shift; <> }; eval qq{ while(<>) { print if $cond } };' lines test.txt #### perl -e'$cond=join"||",map{"($_..\$.==$_+2)"}do{local@ARGV=shift;<>};eval"($cond)&&print while<>"' lines test.txt