使用 Windows 命令行在文本文件中的最大记录长度
Maximum record length in a text file using Windows command line
如何使用 Windows 命令查找文本文件中的最大记录长度。即,它应该输出在该文件中找到的行的最大长度。
对于 wc -L
的批处理实施(将输入文件作为参数提供给批处理)
@echo off
setlocal enableextensions disabledelayedexpansion
set "tempFile=%temp%\%~nx0.%random%.tmp"
for %%f in ("%~1") do (
cmd /c "(type "%%~ff"&echo(&echo()" | findstr /o /r /c:"^" > "%tempFile%"
setlocal enabledelayedexpansion
set /a "max=0", "last=0"
for /f "usebackq delims=:" %%a in ("%tempFile%") do (
set /a "length=%%a-last-2"
set "last=%%a"
if !length! gtr !max! set "max=!length!"
)
echo !max! "%%~ff"
endlocal
del /q "%tempFile%" 2> nul
)
所做的是使用 findstr
生成一个临时文件,其中所有行都以其在文件内的偏移量为前缀。剩下的就是用前一行的偏移量减去当前行的偏移量来计算它的长度。在使用的临时文件中添加了两行(for /f
加载文件比检索命令输出快得多)以确保正确处理文件中的最后一行
仅使用本机 Powershell 实用程序:
Get-Content C:\textfile.txt | Measure-Object -Property length -Maximum
获取最长行的实际内容:
Get-Content C:\textfile.txt | Sort-Object -Property length | Select-Object -last 1
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET /a maxlength=0
FOR /f "delims=" %%a IN (q27898945.txt) DO (
SET "line=%%a"
CALL :calc
)
ECHO maxlength IN file is %maxlength%
GOTO :EOF
:calc
SET "line2=!line:~%maxlength%!"
IF DEFINED line2 set/a maxlength+=1&GOTO calc
GOTO :eof
我使用了一个名为 q27898945.txt
的文件,其中包含一些用于测试的随机文本。
会显示出对内容的一些敏感性 - 例如,包含 %text%
的行将被计算错误。
使用我的 JREPL.BAT utility - 混合 JScript/batch 脚本,可在任何 Windows XP 以后的机器上运行。
jrepl "$" "if ($off>max) max=$off;false" /f test.txt /jbeg "var max=0" /jend "output.WriteLine(max)" /jmatch
如果您想在脚本中使用它,请在 JREPL 前加上 CALL。
如果您想在批处理脚本中捕获变量中的值:
for %%N in (
'call jrepl "$" "if ($off>max) max=$off;false" /f test.txt /jbeg "var max=0" /jend "output.WriteLine(max)" /jmatch'
) do set /a max=%%N
如何使用 Windows 命令查找文本文件中的最大记录长度。即,它应该输出在该文件中找到的行的最大长度。
对于 wc -L
的批处理实施(将输入文件作为参数提供给批处理)
@echo off
setlocal enableextensions disabledelayedexpansion
set "tempFile=%temp%\%~nx0.%random%.tmp"
for %%f in ("%~1") do (
cmd /c "(type "%%~ff"&echo(&echo()" | findstr /o /r /c:"^" > "%tempFile%"
setlocal enabledelayedexpansion
set /a "max=0", "last=0"
for /f "usebackq delims=:" %%a in ("%tempFile%") do (
set /a "length=%%a-last-2"
set "last=%%a"
if !length! gtr !max! set "max=!length!"
)
echo !max! "%%~ff"
endlocal
del /q "%tempFile%" 2> nul
)
所做的是使用 findstr
生成一个临时文件,其中所有行都以其在文件内的偏移量为前缀。剩下的就是用前一行的偏移量减去当前行的偏移量来计算它的长度。在使用的临时文件中添加了两行(for /f
加载文件比检索命令输出快得多)以确保正确处理文件中的最后一行
仅使用本机 Powershell 实用程序:
Get-Content C:\textfile.txt | Measure-Object -Property length -Maximum
获取最长行的实际内容:
Get-Content C:\textfile.txt | Sort-Object -Property length | Select-Object -last 1
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET /a maxlength=0
FOR /f "delims=" %%a IN (q27898945.txt) DO (
SET "line=%%a"
CALL :calc
)
ECHO maxlength IN file is %maxlength%
GOTO :EOF
:calc
SET "line2=!line:~%maxlength%!"
IF DEFINED line2 set/a maxlength+=1&GOTO calc
GOTO :eof
我使用了一个名为 q27898945.txt
的文件,其中包含一些用于测试的随机文本。
会显示出对内容的一些敏感性 - 例如,包含 %text%
的行将被计算错误。
使用我的 JREPL.BAT utility - 混合 JScript/batch 脚本,可在任何 Windows XP 以后的机器上运行。
jrepl "$" "if ($off>max) max=$off;false" /f test.txt /jbeg "var max=0" /jend "output.WriteLine(max)" /jmatch
如果您想在脚本中使用它,请在 JREPL 前加上 CALL。
如果您想在批处理脚本中捕获变量中的值:
for %%N in (
'call jrepl "$" "if ($off>max) max=$off;false" /f test.txt /jbeg "var max=0" /jend "output.WriteLine(max)" /jmatch'
) do set /a max=%%N