|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
 『楼 主』:
"小函数"读取文件指定的特定行(ReadLine2.bat)
使用 LLM 解释/回答一下
在ReadLine.bat的基础上修改了一下,读取文件指定的特定行。
使用方法也比较简单,在批处理中加上 "Call ReadLine2.bat <文件名> <读取行号>" 就可以了,例如:"Call ReadLine2 system.ini 11 5 7 9",可以读取system.ini文件的第5、7、9和11行,后面的行号不分先后大小顺序。但是效率不是很高,读取小文件还是可以的。
ReadLine2.bat
@echo off&SetLocal ENABLEDELAYEDEXPANSION
:::::::::::ReadLine2.Bat::::::::::::::::::
if "%1"=="" (goto --help) else (set file=%1)
if not exist %file% goto :eof
if "%2"=="" (
for /f "tokens=1* delims=:" %%a in ('findstr /n .* "%file%"') do echo/%%b
goto :eof
) else (
set args=%*
for %%a in (!args!) do (
if not "%%a"=="%1" (for /f "tokens=1* delims=:" %%b in ('findstr /n .* "%file%"') do (
if "%%b"=="%%a" echo/%%c)
)
)
)
goto :eof
:--help
echo/======================================
echo/本程序段需要带参数才能正常运行
echo/&echo/Usage:&echo/Call ReadLine2 ^<文件名^> ^<读取行号^>
echo/&echo/例如:Call ReadLine2 aa.txt 5 7 11 ,将读取aa.txt文件的第5,7,11行
echo/&echo/如果^<读取行号^>没有指定,将读取整个文件的内容
echo/======================================
goto :eof
Modified based on ReadLine.bat to read specific lines from a file.
The usage is also relatively simple. Add "Call ReadLine2.bat <file name> <line number to read>" in the batch script. For example: "Call ReadLine2 system.ini 11 5 7 9" can read lines 5, 7, 9, and 11 of the system.ini file. The line numbers behind do not have a strict order of size. But the efficiency is not very high, and it is still acceptable for reading small files.
ReadLine2.bat
@echo off&SetLocal ENABLEDELAYEDEXPANSION
:::::::::::ReadLine2.Bat::::::::::::::::::
if "%1"=="" (goto --help) else (set file=%1)
if not exist %file% goto :eof
if "%2"=="" (
for /f "tokens=1* delims=:" %%a in ('findstr /n .* "%file%"') do echo/%%b
goto :eof
) else (
set args=%*
for %%a in (!args!) do (
if not "%%a"=="%1" (for /f "tokens=1* delims=:" %%b in ('findstr /n .* "%file%"') do (
if "%%b"=="%%a" echo/%%c)
)
)
)
goto :eof
:--help
echo/======================================
echo/This program segment needs parameters to run normally
echo/&echo/Usage:&echo/Call ReadLine2 ^<file name^> ^<line number to read^>
echo/&echo/For example: Call ReadLine2 aa.txt 5 7 11, which will read lines 5, 7, 11 of the aa.txt file
echo/&echo/If ^<line number to read^> is not specified, the entire content of the file will be read
echo/======================================
goto :eof
|
|
2007-3-20 03:34 |
|
|
baomaboy
银牌会员
    
积分 1513
发帖 554
注册 2005-12-30
状态 离线
|
|
2007-3-20 03:51 |
|
|
HUNRYBECKY
银牌会员
    
积分 1179
发帖 442
注册 2006-9-9
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
lxmxn斑竹怎么一贴发两个地方呀,为了管理方便,建议移到原来的ReadLine1.bat下.谢谢
Moderator lxmxn, why did you post in two places? For the convenience of management, it is suggested to move it under the original ReadLine1.bat. Thanks
|
|
2007-3-20 21:33 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
Originally posted by HUNRYBECKY at 2007-3-20 08:33:
lxmxn斑竹怎么一贴发两个地方呀,为了管理方便,建议移到原来的ReadLine1.bat下.谢谢
这个不叫“一贴发两个地方”吧?
虽然都是用来读取文件指定行的,但是具体作用是不一样的。一个是读取连续行的,一个是读取特定行的,效果也不一样。
Originally posted by HUNRYBECKY at 2007-3-20 08:33:
Why did the moderator lxmxn post the same content in two places? For the convenience of management, it is suggested to move it to the original ReadLine1.bat. Thanks
Isn't this called "posting in two places"?
Although both are used to read specified lines of a file, their specific functions are different. One is to read consecutive lines, and the other is to read specific lines, and the effects are also different.
|
|
2007-3-20 23:17 |
|
|
29984365
初级用户
 
积分 39
发帖 15
注册 2007-4-4
状态 离线
|
|
2007-4-7 04:42 |
|
|
z060053
新手上路

积分 4
发帖 2
注册 2008-7-25
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
@echo off
setlocal EnableDelayedExpansion
set loop=0
set /a skipline=%2-1
for /f "tokens=1 skip=%skipline% delims=" %%a in (%1) do (
set /a loop=!loop!+1
if "!loop!" leq "%3" (echo %%a) else goto :eof
)
goto :eo
@echo off
setlocal EnableDelayedExpansion
set loop=0
set /a skipline=%2-1
for /f "tokens=1 skip=%skipline% delims=" %%a in (%1) do (
set /a loop=!loop!+1
if "!loop!" leq "%3" (echo %%a) else goto :eof
)
goto :eo
|
|
2008-7-25 14:07 |
|