Windows批处理命令(S)来读取文本文件的第一行 [英] Windows batch command(s) to read first line from text file
本文介绍了Windows批处理命令(S)来读取文本文件的第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能读取使用Windows批处理文件的文本文件的第一行?由于该文件是大我只想处理的第一行。
How can I read the first line from a text file using a Windows batch file? Since the file is large I only want to deal with the first line.
推荐答案
下面是一个通用的批处理文件打印顶部 N
从像GNU <文件行code>头工具,而不是只是一个单一的线。
Here's a general-purpose batch file to print the top n
lines from a file like the GNU head
utility, instead of just a single line.
@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的批处理文件线长度的限制。
It does not currently count blank lines. It is also subject to the batch-file line-length restriction of 8 KB.
这篇关于Windows批处理命令(S)来读取文本文件的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文