批处理命令采取从仅输入第一行 [英] Batch command to take only first line from input

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

问题描述

我在找一个DOS批处理程序,需要的文件:

I'm looking for a DOS batch program that takes a file:

First input line
Second input line
Third input line...

和输出的第一行输入

推荐答案

假设你的意思是在Windows CMD 间preTER(我会感到惊讶,如果你真的的的仍然使用DOS),下面的脚本会做你想要什么:

Assuming you mean the Windows cmd interpreter (I'd be surprised if you really were still using DOS), the following script will do what you want:

@echo off
setlocal enableextensions enabledelayedexpansion
set first=1
for /f "delims=" %%i in (infile.txt) do (
    if !first!==1 echo %%i
    set first=0
)
endlocal

使用 infile.txt 的输入文件为:

line 1
line 2
line 3

这将输出:

line 1

这仍然过程的所有行,它只是不会。如果你想真正停止处理打印那些超越1号线,使用这样的:

This will still process all the lines, it just won't print those beyond line 1. If you want to actually stop processing, use something like:

@echo off
setlocal enableextensions enabledelayedexpansion
for /f "delims=" %%i in (infile.txt) do (
    echo %%i
    goto :endfor
)
:endfor
endlocal

或者你可以只去把你的手放在 Cygwin的或的的GnuWin32 ,并使用程序。这就是我会做。但是,如果这不是一个选项(有些工作场所不允许它),你可以按如下( winhead.cmd )创建Windows本身相似的CMD文件:

Or you could just go get your hands on Cygwin or GnuWin32 and use the head program. That's what I'd do. But, if that's not an option (some workplaces don't allow it), you can create a similar cmd file in Windows itself as follows (winhead.cmd):

@echo off
setlocal enableextensions enabledelayedexpansion

if x%1x==xx goto :usage
if x%2x==xx goto :usage

set /a "linenum = 0"
for /f "usebackq delims=" %%i in (%1) do (
    if !linenum! geq %2 goto :break1
    echo %%i
    set /a "linenum = linenum + 1"
)
:break1
endlocal
goto :finish

:usage
echo.winhead ^<file^> ^<numlines^>
echo.   ^<file^>
echo.      is the file to process
echo.      (surround with double quotes if it contains spaces).
echo.   ^<numlines^>
echo.      is the number of lines to print from file start.
goto :finish

:finish
endlocal

这篇关于批处理命令采取从仅输入第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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