windows批处理控制台中的for循环挂起,在处理10 gb的大型文件时不工作,但对小于1GB的较小文件工作。例如
FOR /F "delims=]" %%A IN (dummy.txt) DO (
...code...
)我需要一个批处理脚本,它将有效地从10 gb Informatica会话日志文件中读取代码的前10行。是否有任何方法可以使用批处理程序读取大型文件??
发布于 2018-12-03 14:42:00
试试这个:
@echo off
setlocal EnableDelayedExpansion
rem Read first 10 lines
(for /L %%i in (1,1,10) do set /P "line[%%i]=") < dummy.txt
rem Process them
for /L %%i in (1,1,10) do (
echo Line %%i- !line[%%i]!
FOR /F "delims=]" %%A IN ("!line[%%i]!") DO (
...code...
)
)PS -我敢打赌这个方法比PowerShell one运行得快得多!
发布于 2018-12-03 11:43:16
我建议你用powershell来做这个。有两种基本方法:打开powershell。Start,run(search),poweshell.exe。然后将cd转到相关目录,然后运行:
Get-Content dummy.txt | Select-Object -first 10或者将上面的内容保存在一个具有.ps1扩展名的文件中,然后从那里运行它。
如果您仍然希望在批处理文件中使用它,只需添加一些内容,如下面所示,并将其保存为.bat或.cmd文件。
powershell -command "& {Get-Content dummy.txt | Select-Object -first 10}"如果有powershell 3或更高版本,则可以使用内置的新head函数。
分批:
powershell -command "& {Get-Content dummy.txt -Head 10}"或者再一次作为纯powershell:
Get-Content dummy.txt -Head 10发布于 2018-12-03 21:02:02
要从一个大文件中读取数据,可以使用 loop并使用goto中止它
@echo off
set /A "COUNT=0, LIMIT=10"
for /F usebackq^ delims^=^ eol^= %%L in ("huge.txt") do (
set /A "COUNT+=1" & set "LINE=%%L"
setlocal EnableDelayedExpansion
rem // Do something with the current line:
echo Line !COUNT!: !LINE!
if !COUNT! geq !LIMIT! goto :QUIT
endlocal
)
:QUIT每一行的最大长度约为8190字节或字符。空行被for /F忽略,因此不计数。
https://stackoverflow.com/questions/53592263
复制相似问题