How to print first 5 lines of the file in windows cmd

12,923

Solution 1

You should be able to use the more command in some regard.

Maybe "more filename P 5"

see http://support.microsoft.com/kb/227449

Solution 2

Alright, it might be like replying to a very old thread, but still for anyone who would benefit from this.
Use findstr

findstr /n . yourfile.txt  | findstr "^[0-5]:"

The breakdown is-
first findstr prints the line number where it finds any character (denoted by a literal dot), second findstr will print lines that start ( using ^ for this ) with any number ranging from 0 through 5. Also include ":" there to avoid findstr matching with recurring digits like 12,13,14... 21,22,23..., etc.
^[0-5]: will match only for lines starting with-

1:
2:
3:
4:
5:

I tested it on a 250 MB file and it worked within a second.

The same can be achieved in linux using sed-

sed -n '1,5p' yourfile.txt
Share:
12,923
Jade
Author by

Jade

Python/Django developer

Updated on June 14, 2022

Comments

  • Jade
    Jade almost 2 years

    How can I print first 5 lines of the file in windows CMD?

    There is a problem that I can't use PowerShell (spicific task) and batch scripts. Can you help me with this?