分享

从文本文件中读取特定的行

 JT_man 2012-04-01

>>This page shows how to read specific lines from a text file. There are many ways to have the for /f command read the input file, for instance:
这篇文章演示如何从文件中读取特定的行.用for /f命令的多种变化方式可以达到该目的,例如:
---------------------------------for /f-----------------------------------------
for /f "delims=" %%a in (input.txt) do ...
for /f "delims=" %%a in ('type input.txt') do ...
for /f "delims=" %%a in ('more ^< input.txt') do ...
---------------------------------for /f-----------------------------------------

>>However, only the last method (using the more command) will give consistent results across Windows NT, 2000, XP and 2003. The first method does not recognise unicode files. Also, the usebackq switch must be used if the input filename contains spaces. The second method, using the type command, also fails to recognise unicode files on Windows 2000, XP and 2003 if the input file does not begin with a bit order mark (BOM).
然而,只有最后一种方法(用到more命令的)在windows nt,2000,xp和2003中能够得到一致的结果。第一种方法,不能有效的识别unicode编码文件,同时,如果文件名含有空格时,usebackq开关必须打开。第二种方法,用到type命令,同样在windows nt,2000,xp和2003中,不能有效识别unicode编码文件,同时,要求输入的文件内容不能以位序标志(BOM)开头.

>>In all the examples, assume the contents of of the file numbers.txt to be:
在如下所有的例子中,假设numbers.txt文件输入以下内容:
----------------------numbers.txt--------------------------
one
two
three
four
five
six
seven
eight
nine
ten
----------------------numbers.txt--------------------------

JT_man注:以下代码中的setlocal ENABLEEXTENSIONS都可以删除,因为系统默认命令处理器扩展名是启动的。

>>Displaying the first line,This example prints one.
显示第一行,打印第一行的例子如下:
------------------------1.bat------------------------------------------
@echo off & 
setlocal ENABLEEXTENSIONS 
set "first="
for /f "delims=" %%a in ('more ^< numbers.txt') do (
if not defined first set first=%%a
)
echo/%first%
pause>nul
------------------------1.bat-------------------------------------------

>>Displaying the first X lines,This example prints one, two and three.
显示前几行,打印第一,二,三行的例子如下:
------------------------123.bat-------------------------------
@echo off & setlocal ENABLEEXTENSIONS
set "lines=3"
set i=-1
set "ok="
for /f "delims=" %%a in ('more ^< numbers.txt') do (
      set/a i+=1 & for /f %%z in ('echo/%%i%%') do (
      if "%%z"=="%lines%" set ok=1
      )
if not defined ok echo/%%a
)
pause>nul
------------------------123.bat-------------------------------

JT_man注:或者
------------------------123_2.bat------------------------------- 
@echo off
setlocal enabledelayedexpansion
set "lines=3"
rem 显示前3行
set i=-1  
set "ok="
for /f "delims=" %%a in ('more ^< numbers.txt') do (
      set /a i+=1
      if !i!==%lines% set ok=1
      if not defined ok echo %%a
pause>nul 
------------------------123_2.bat------------------------------- 

>>Displaying the last line,This example prints ten.
显示最后一行,打印第十行的例子如下:
------------------------10.bat--------------------------------------------
@echo off & setlocal ENABLEEXTENSIONS
for /f "delims=" %%a in ('more ^< numbers.txt') do set "last=%%a"
echo/%last%
pause>nul
------------------------10.bat--------------------------------------------

>>Displaying the last X lines,This example prints nine and ten.
显示最后x行,打印第9,10行的例子如下:
------------------------lastx.bat-----------------------------------------------
@echo off & setlocal ENABLEEXTENSIONS
set "lines=2"
for /f %%a in ('find/c /v "" ^< numbers.txt') do set/a skip=%%a-lines
for /f "delims=" %%a in ('more/e +%skip% ^< numbers.txt') do (
:: 或者 for /f "skip=%skip% delims=" %%a in ('more ^< numbers.txt') do ( 
echo/%%a
)
pause>nul
------------------------lastx.bat------------------------------------------------

>>Displaying the Nth line,This example prints three.
Note that instead of using the more command's /e switch, the skip option could have been used with the for /f command, however, this fails is it is set to any number less than one.
显示第n行,打印第三行.
注意启用more命令的扩展功能(/e)开关,同时,ship选项过去常用于for /f命名中,但是当设定的数目小于实际值时,将导致失败
------------------------Nth.bat-----------------------------------------------
@echo off & setlocal ENABLEEXTENSIONS
set LineNo=3
set "line="
set/a LineNo-=1
for /f "delims=" %%a in ('more/e +%LineNo% ^< numbers.txt') do (
if not defined line set "line=%%a"
)
echo/%line%
pause>nul
------------------------Nth.bat------------------------------------------------

>>Displaying the Nth line plus X number of lines,This example prints five and six.
显示第n+x行,打印第5,6行的例子如下:
------------------------x+Nth.bat--------------------------------------------------
@echo off & setlocal ENABLEEXTENSIONS
set start=5
set "lines=2"
set/a i=-1,start-=1
set "ok="
for /f "delims=" %%a in ('more/e +%start% ^< numbers.txt') do (
set/a i+=1 & for /f %%z in ('echo/%%i%%') do (
if "%%z"=="%lines%" set ok=1
)
if not defined ok echo/%%a
)
pause>nul
------------------------x+Nth.bat----------------------------------------------------

JT_man注:或者
------------------------通用.bat-------------------------------------------------- 
@echo off
setlocal enabledelayedexpansion

set start=5
rem 开始显示的行号
set "lines=2"
rem 要显示的行数

set/a i=-1,start-=1
set "ok=" 
for /f "skip=%start% delims=" %%a in ('more ^< numbers.txt') do (
      set/a i+=1
      if "!i!"=="%lines%" set ok=1
      if not defined ok echo/%%a
)
pause>nul 
------------------------通用.bat-------------------------------------------------- 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多