【发布时间】:2010-09-12 21:46:39
【问题描述】:
如何使用 Windows 批处理文件从文本文件中读取第一行?由于文件很大,我只想处理第一行。
【问题讨论】:
-
试试 GNU32 “head” 实用程序。不要以为只用 DOS Batch 就能轻松完成你所追求的。
-
@Nasir ,不能用内置命令完成吗?
标签: batch-file cmd
如何使用 Windows 批处理文件从文本文件中读取第一行?由于文件很大,我只想处理第一行。
【问题讨论】:
标签: batch-file cmd
嗯? imo这要简单得多
set /p texte=< file.txt
echo %texte%
【讨论】:
echo %texte:~3% 例如将跳过前三个字符。这在您使用 BOM 读取 UTF-8 文件时很有用。
这是一个通用批处理文件,用于从 GNU head 实用程序等文件中打印顶部的 n 行,而不仅仅是一行。
@echo off
if [%1] == [] goto usage
if [%2] == [] goto usage
call :print_head %1 %2
goto :eof
REM
REM print_head
REM Prints the first non-blank %1 lines in the file %2.
REM
:print_head
setlocal EnableDelayedExpansion
set /a counter=0
for /f ^"usebackq^ eol^=^
^ delims^=^" %%a in (%2) do (
if "!counter!"=="%1" goto :eof
echo %%a
set /a counter+=1
)
goto :eof
:usage
echo Usage: head.bat COUNT FILENAME
例如:
Z:\>head 1 "test file.c"
; this is line 1
Z:\>head 3 "test file.c"
; this is line 1
this is line 2
line 3 right here
目前不计算空行。它还受制于 8 KB 的批处理文件行长度限制。
【讨论】:
; 开头的行,这是默认的 FOR /F EOL 字符。如果要求 10 行,那么它将打印前 10 行不为空且不以 ; 开头的行。
你们……
C:\>findstr /n . c:\boot.ini | findstr ^1:
1:[boot loader]
C:\>findstr /n . c:\boot.ini | findstr ^3:
3:default=multi(0)disk(0)rdisk(0)partition(1)\WINNT
C:\>
【讨论】:
findstr "^1:" 并获得双引号的温暖和保护。或者,如果你鄙视像我这样的引用并想危险地生活,请使用findstr /b 1:
findstr ^^1。
set /p 解决方案对于大文件的效率要高得多。
你可以试试这个:
@echo off
for /f %%a in (sample.txt) do (
echo %%a
exit /b
)
编辑 或者,假设您有四列数据并且想要从第 5 行向下到底部,试试这个:
@echo off
for /f "skip=4 tokens=1-4" %%a in (junkl.txt) do (
echo %%a %%b %%c %%d
)
【讨论】:
"delims=" 才能打印出完整的文件夹名称和空格。
感谢 thetalkingwalnut 的回答 Windows batch command(s) to read first line from text file 我想出了以下解决方案:
@echo off
for /f "delims=" %%a in ('type sample.txt') do (
echo %%a
exit /b
)
【讨论】:
稍微建立在其他人的答案之上。现在允许您指定要从中读取的文件以及要将结果放入的变量:
@echo off
for /f "delims=" %%x in (%2) do (
set %1=%%x
exit /b
)
这意味着您可以像这样使用上面的内容(假设您将其命名为 getline.bat)
c:\> dir > test-file
c:\> getline variable test-file
c:\> set variable
variable= Volume in drive C has no label.
【讨论】:
一个衬里,用于使用 ">" 进行标准输出重定向:
@for /f %%i in ('type yourfile.txt') do @echo %%i & exit
【讨论】:
powershell Get-Content file.txt -Head 1
这个比上面读取完整文件的其他 powershell 示例快得多。
【讨论】:
试试这个
@echo off
setlocal enableextensions enabledelayedexpansion
set firstLine=1
for /f "delims=" %%i in (yourfilename.txt) do (
if !firstLine!==1 echo %%i
set firstLine=0
)
endlocal
【讨论】:
循环文件(file1.txt、file1[1].txt、file1[2].txt 等):
START/WAIT C:\LAERCIO\DELPHI\CICLADOR\dprCiclador.exe C:\LAERCIUM\Ciclavel.txt
rem set/p ciclo=< C:\LAERCIUM\Ciclavel.txt:
set/p ciclo=< C:\LAERCIUM\Ciclavel.txt
rem echo %ciclo%:
echo %ciclo%
它正在运行。
【讨论】:
set /p通过提示询问;但是,通过文件重定向<,它会立即在提示符处获取文件的内容;并且由于第一行以换行结束,此时提示停止读取,因此仅将第一行存储在变量中。
EXIT /B 解决方案的问题,更现实地是在批处理文件中作为它的一部分,如下所示。在EXIT /B之后,所述批处理文件内没有后续处理。通常,批次不仅仅是一项有限的任务。
为了解决这个问题:
@echo off & setlocal enableextensions enabledelayedexpansion
set myfile_=C:\_D\TEST\My test file.txt
set FirstLine=
for /f "delims=" %%i in ('type "%myfile_%"') do (
if not defined FirstLine set FirstLine=%%i)
echo FirstLine=%FirstLine%
endlocal & goto :EOF
(不过,所谓的毒字还是会有问题的。)
关于使用批处理命令获取特定行的更多信息:
如何获取文本文件的第 n 行、第一行和最后一行?" http://www.netikka.net/tsneti/info/tscmd023.htm
[2012 年 8 月 28 日添加] 还可以:
@echo off & setlocal enableextensions
set myfile_=C:\_D\TEST\My test file.txt
for /f "tokens=* delims=" %%a in (
'type "%myfile_%"') do (
set FirstLine=%%a& goto _ExitForLoop)
:_ExitForLoop
echo FirstLine=%FirstLine%
endlocal & goto :EOF
【讨论】:
这是使用powershell 的解决方法:
powershell (Get-Content file.txt)[0]
(您还可以使用powershell (Get-Content file.txt)[0..3] 轻松阅读一系列行)
如果您需要在批处理脚本中设置一个变量作为file.txt 的第一行,您可以使用:
for /f "usebackq delims=" %%a in (`powershell ^(Get-Content file.txt^)[0]`) do (set "head=%%a")
【讨论】:
请注意,批处理文件方法将仅限于 DOS 命令处理器的行数限制 - 请参阅What is the command line length limit?。
因此,如果尝试处理包含多于 8192 个字符的行的文件,则脚本将跳过它们,因为无法保留该值。
【讨论】:
另一种方式
setlocal enabledelayedexpansion
@echo off
for /f "delims=" %%i in (filename.txt) do (
if 1==1 (
set first_line=%%i
echo !first_line!
goto :eof
))
【讨论】:
在 Windows PowerShell 中,下面的 cmd 可用于获取第一行并将其替换为静态值
powershell -Command "(gc txt1.txt) -replace (gc txt1.txt)[0], 'This is the first line' | Out-File -encoding ASCII txt1.txt"
参考
How can you find and replace text in a file using the Windows command-line environment?
【讨论】:
仅打印第一行(无需读取整个文件):
set /p a=< file.txt & echo !a!
要打印第一行,然后等待用户按下下一行的键:
(打印需要的行后,按Ctrl+C停止。)
for /f "delims=" %a in (downing.txt) do echo %a & pause>nul
要打印第一 n 行(无需用户按键):
type nul > tmp & fc tmp "%file%" /lb %n% /t | find /v "*****" | more +2
在 Win 10 CMD 上测试。
【讨论】: