Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
693 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...