exit after extracting range if lines - awk

Hello,

I was wondering how is it possible if I use this command:

awk 'NR >= 998 && NR <= 1000' file.txt

To exit after parsing the 1000th line ( last line targeted) ???

I observed that when executing this command for a large file, if the range of lines is at the beginning of the file it is quickly extracted, but awk will parse the entire file, and only after reaching the end of file it exits.

Same question goes for sed :

sed -n 'xx,yy p' file
awk 'NR>=998;NR==1000{exit}' file
sed -n '998,1000{;p;1000q;}' file
2 Likes

Hello, yes this is what i was looking for.

Thanks!