从文本文件中读取第一行的 Windows 批处理命令 [英] Windows batch command(s) to read first line from text file

查看:43
本文介绍了从文本文件中读取第一行的 Windows 批处理命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 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.

推荐答案

这是一个通用的批处理文件,用于打印 GNU headn 行> 实用程序,而不仅仅是一行.

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 批处理命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆