使用单个 windows 命令从文件中提取 N 行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27932722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extract N lines from file using single windows command
提问by user1769292
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?
有没有办法从文件中提取/复制第一个 X 行,然后使用 Windows 命令提示符使用单个命令将它们输入到另一个文件中?
I can delete the first X number of lines using:
more +X [file_containing data] > [file_to_export_data_to]
我可以使用以下方法删除前 X 行:
more +X [file_包含数据] > [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]
如果 head 命令可以工作,我想我可以这样做:
head -X [file_包含数据] > [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.
如果 Windows 有一个“less”命令但又没有运气,那就太好了。
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
谢谢
回答by dbenham
You can use PowerShell from the cmd.exe console:
您可以从 cmd.exe 控制台使用 PowerShell:
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 宏以使其更易于从命令行使用:
doskey head=powershell -command "& {get-content |select-object -first }"
Usage:
用法:
head input.txt 10 >output.txt
But you cannot use a DOSKEY macro within a batch script.
但是您不能在批处理脚本中使用 DOSKEY 宏。
You could create a head.bat script instead and place it in a folder that is included in your PATH:
您可以创建一个 head.bat 脚本,并将其放置在您的 PATH 中包含的文件夹中:
head.bat
头.bat
@powershell -command "& {get-content %1|select-object -first %2}"
From the command line, you would use head input.txt 10 >output.txt
从命令行,您将使用 head input.txt 10 >output.txt
From within a batch script, you would use call head input.txt 10 >output.txt
在批处理脚本中,您将使用 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.
我选择不将输出文件作为参数,以防您只想将结果显示到屏幕而不是写入文件。
回答by Janusz
the simplest one-command solution is to use Powershell Get-Content.
最简单的单命令解决方案是使用 Powershell Get-Content。
N - number of lines.
N - 行数。
From the begining of file:
从文件开头:
Get-Content -Head N file.txt
From the end of file:
从文件末尾:
Get-Content -Tail N file.txt
回答by Magoo
(@FOR /f "tokens=1* delims=:" %a IN ('findstr /n "^" "standardwaffle.txt"') DO @IF %a leq 7 ECHO(%b)>u:\junk.txt
would extract the first 7 lines of standardwaffle.txt
to u:\junk.txt
so here it is in one cmd
line - but I'd defy you to enter that reliably.
会提取standardwaffle.txt
to的前 7 行,u:\junk.txt
所以这里是cmd
一行 - 但我拒绝你可靠地输入。
It would also remove any leading :
on a source line.
它还会删除:
源代码行上的任何前导。
@ECHO OFF
SETLOCAL
IF %1 lss 0 (SET /a line=-%1) ELSE (SET /a line=%1)
FOR /f "tokens=1* delims=:" %%a IN ('findstr /n "^" "%~2"') DO IF %%a leq %line% ECHO(%%b
GOTO :EOF
This batch, saved as head.bat
placed anywhere on your path would allow you to use
该批次保存为head.bat
放置在您路径上的任何位置,您可以使用
head -n standardwaffle.txt >junk.txt
to extract the first n
lines of standardwaffle.txt
to junk.txt
以提取所述第一n
线standardwaffle.txt
到junk.txt
the -
would be optional
该-
会是可选
butthis involves installing the batch on your machine. Is that banned by your "no installing" requirement, or is "installing" meant only for 3rd party utilities?
但这涉及在您的机器上安装批处理。这是否被您的“不安装”要求所禁止,还是“安装”仅适用于 3rd 方实用程序?
回答by Serenity
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
x = 0
Do Until Inp.AtEndOfStream
x = x + 1
OutP.WriteLine Inp.Readline
If x = 5 then Exit Do
Loop
This prints lines 1 to 5. To use
这将打印第 1 到 5 行。使用
cscript //nologo <path to script.vbs> <inputfile >outputfile
回答by sdk
In order to get correct utf8 output, do the following in powershell
为了获得正确的 utf8 输出,请在 powershell 中执行以下操作
chcp 65001
$OutputEncoding = New-Object -typename System.Text.UTF8Encoding
get-content input.txt -encoding UTF8 |select-object -first 10000 > output.txt
This will get first 10000 lines of input.txt (file in utf8 format) to output.txt with correct encoding.
这将使用正确的编码将前 10000 行 input.txt(utf8 格式的文件)输出到 output.txt。
回答by npocmaka
you can use this:
你可以使用这个:
break>"%temp%\empty"&&fc "%temp%\empty" "%file_to_process%" /lb X /t |more +4 | findstr /B /E /V "*****"
where you should replace the Xwith the lines you want.Or name this head.bat
:
您应该将X替换为您想要的行。或者命名为head.bat
:
break>"%temp%\empty"&&fc "%temp%\empty" "%file_to_process%" /lb %~1 /t |more +4 | findstr /B /E /V "*****"
回答by Chris Clews
If you wanted to stick to simple Windows commands you could use this but would be a little slow for large files ;-) (I've added a second solution below that works better :-) this extracts the last 100 records of any length of file)
如果您想坚持使用简单的 Windows 命令,您可以使用它,但对于大文件来说会有点慢;-)(我在下面添加了第二个解决方案,效果更好:-) 这将提取任意长度的最后 100 条记录文件)
find /n " " <test.txt >test.tmp
for /l %%i in (1,1,100) do find "[%%i]" <test.tmp >test.tmp2
for /f "delims=] tokens=2" %%i in (test.tmp2) do echo %%i >>test.new
del test.tmp
del test.tmp2
move /y test.new test.txt
find /v /n "" <test.txt >test.tmp
for /f "delims=: tokens=2 %%i in ('find /v /c "" test.txt') do set /a NR=%%i
set /a NS=%NR%-100
for /l %%i in (%NS%, 1, %NR%) do find "[%%i]" <test.tmp >>test.tmp2
for /f %%i "delims=] tokens=2 %%i in (test.tmp2) do echo %%i >>test.new
move /y test.new test.txt
回答by Zimba
A single line example to extract first 9 lines from file.txt & write into nine.txt
从 file.txt 中提取前 9 行并写入九个.txt 的单行示例
for /f "tokens=* delims=[" %i in ('type "file.txt" ^| find /v /n "" ^| findstr /b /r \[[1-9]\]') do set a=%i& set a=!a:*]=]!& echo:!a:~1!>> nine.txt
Preserves blank lines, lines starting with semicolon, leading spaces and preserves delimiter and whitespaces.
保留空行、以分号开头的行、前导空格并保留分隔符和空格。
Tested on Win 10 x64 CMD
在 Win 10 x64 CMD 上测试