Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

cmd - Extract N lines from file using single windows command

Is there a way to extract/copy the fist X number of lines from a file and input them into another file with a single command using the windows command prompt?

I can delete the first X number of lines using:
more +X [file_containing data] > [file_to_export_data_to]

If the head command would work I think I could just do this:
head -X [file_containing data] > [file_to_export_data_to]

But that unfortunately does not work.

It would be great if Windows had a "less" command but again no luck.

I'm a complete novice when it comes to this stuff so I'm sure I'm missing something obvious. I don't want to install anything or use something other than the command prompt.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

the simplest one-command solution is to use Powershell Get-Content.

N - number of lines.

From the begining of file:

Get-Content -Head N file.txt

From the end of file:

Get-Content -Tail N file.txt

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...