使用单个窗口命令从文件提取N行 [英] Extract N lines from file using single windows command

查看:156
本文介绍了使用单个窗口命令从文件提取N行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从一个文件中提取/复制第一行X行数,并使用Windows命令提示符用单个命令将它们输入到另一个文件中?



我可以使用以下命令删除前X个行:

more + X [file_containing data]> [file_to_export_data_to]



工作我想我可以这样做:

头-X [file_containing data]> [file_to_export_data_to]



但是,不幸的是不工作。 / p>

这将是伟大的,如果Windows有一个少命令,但再没有运气。



我是一个完整的新手,当谈到这个东西,所以我确定我缺少一些明显的东西。我不想安装任何东西或使用命令提示符以外的其他东西。



感谢

解决方案

您可以从cmd.exe控制台使用PowerShell:

  powershell -command& get-content input.txt | select-object -first 10}> output.txt 

可以创建一个DOSKEY宏,使其更容易从命令行使用:

  doskey head = powershell -command& get-content $ 1 | select-object -first $ 2}

用法:

  head input.txt 10> output.txt 

但是您不能在批处理脚本中使用DOSKEY宏。



您可以创建一个head.bat脚本,包含在您的PATH中:



head.bat

 code> @powershell -command& {get-content%1 | select-object -first%2}

从命令行,您将使用 head input.txt 10> output.txt



在批处理脚本中,您可以使用调用head input.txt 10> output.txt



我选择不将输出文件作为参数,以便您只想将结果显示到屏幕,而不是写入文件。


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

解决方案

You can use PowerShell from the cmd.exe console:

 powershell -command "& {get-content input.txt|select-object -first 10}" >output.txt

You could create a DOSKEY macro to make it easier to use from the command line:

doskey head=powershell -command "& {get-content $1|select-object -first $2}"

Usage:

head input.txt 10 >output.txt

But you cannot use a DOSKEY macro within a batch script.

You could create a head.bat script instead and place it in a folder that is included in your PATH:

head.bat

@powershell -command "& {get-content %1|select-object -first %2}"

From the command line, you would use head input.txt 10 >output.txt

From within a batch script, you would use call head input.txt 10 >output.txt

I chose not to have the output file as a parameter in case you want to simply display the result to the screen instead of writing to a file.

这篇关于使用单个窗口命令从文件提取N行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆