66

使用单个Windows命令从文件中提取N行

 5 years ago
source link: https://codeday.me/bug/20190112/511092.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
有没有办法从文件中提取/复制第一行X行,并使用 windows

命令提示符使用单个命令将它们输入到另一个文件中?

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

更多X [file_containing data]> [file_to_export_data_to]

如果head命令可行,我想我可以这样做:

head -X [file_containing data]> [file_to_export_data_to]

但不幸的是,这不起作用.

如果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

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

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

在批处理脚本中,您将使用call head input.txt 10> output.txt

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

翻译自:https://stackoverflow.com/questions/27932722/extract-n-lines-from-file-using-single-windows-command


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK