难道懂E文的朋友都没看到?
看来只好用我拙劣的水平试试看,希望能抛砖引玉。
请大家批评指正但不要笑话,我脸皮很薄的。^_^
这一页告诉大家如何从一个文本文件读取某一行. 这里有很多方法可以使用 for/f 读取 input.txt 的内容,例如:
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 ...
不过,只有最后一种方法(使用 more 命令) 可以在不同的操作系统中如:Windows NT, 2000, XP and 2003 取得一致的结果。第一种方法不能识别UNICODE编码文件,并且, 如果输入文件名包含空格,usebackq 参数必须被使用,第二个方法, 使用 type 命令, 在 windows2000, XP 和 2003 中如果文件不是从字节序标记(BOM)开始 ,同样不识别unicode 文件。
在所有例子中,假设numbers.txt的内容为:
one
two
three
four
five
six
seven
eight
nine
ten
显示第一行
这个例子输出 one。
@echo off & setlocal ENABLEEXTENSIONS
set "first="
for /f "delims=" %%a in ('more ^< numbers.txt') do (
if not defined first set first=%%a
)
echo/%first%
显示前几行
这个例子输出 one, two 和 three。
@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
)
显示最后一行
这个例子输出 ten。
@echo off & setlocal ENABLEEXTENSIONS
for /f "delims=" %%a in ('more ^< numbers.txt') do set "last=%%a"
echo/%last%
显示最后几行
这个例子输出 nine 和 ten。
@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 (
echo/%%a
)
显示第 n 行
这个例子输出three,注意这里使用more的 /e 参数 来跳过指定的行数,配合for/f 使用,当数值小于1时会失败。
@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%
显示第 n 行加上 X 行
这个例子输出five和six。
@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
)
Don't the friends who understand English see it?
It seems I have to try with my poor level, hoping to start a discussion.
Please criticize and correct but don't laugh, my skin is very thin. ^_^
This page tells everyone how to read a certain line from a text file. There are many methods to use for/f to read the content of input.txt, for example:
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 ...
However, only the last method (using the more command) can get consistent results in different operating systems such as Windows NT, 2000, XP and 2003. The first method cannot recognize UNICODE-encoded files, and if the input file name contains spaces, the usebackq parameter must be used. The second method, using the type command, also does not recognize UNICODE files in Windows 2000, XP and 2003 if the file does not start with a byte order mark (BOM).
In all examples, assume the content of numbers.txt is:
one
two
three
four
five
six
seven
eight
nine
ten
Display the first line
This example outputs one.
@echo off & setlocal ENABLEEXTENSIONS
set "first="
for /f "delims=" %%a in ('more ^< numbers.txt') do (
if not defined first set first=%%a
)
echo/%first%
Display the first few lines
This example outputs one, two and three.
@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
)
Display the last line
This example outputs ten.
@echo off & setlocal ENABLEEXTENSIONS
for /f "delims=" %%a in ('more ^< numbers.txt') do set "last=%%a"
echo/%last%
Display the last few lines
This example outputs nine and ten.
@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 (
echo/%%a
)
Display the nth line
This example outputs three. Note that the /e parameter of more is used here to skip the specified number of lines, and when used with for/f, it will fail if the value is less than 1.
@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%
Display the nth line plus X lines
This example outputs five and six.
@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
)