我的文本文件包含23行(行包括:!@$:/;“)
我怎么能只显示第3行?在还是七号?还是19岁?
我尝试了所有的命令都是堆叠溢出。
示例:
setlocal enabledelayedexpansion
@echo off
for /f "delims=" %%i in (mytext.txt) do (
if 1==1 (
set first_line=%%i
echo !first_line!
goto :eof
))这只是第一行
发布于 2018-05-24 15:35:02
@Compo给出了一个很好的答案。这只是为了说明这一点。使用像GC这样的别名不应该放在脚本中。当然,在命令行中,如果您愿意,可以继续减少输入。此外,拼写参数名称提供了更多的信息和帮助更快的理解。
只得到第3行。
GC .\mytext.txt -T 3|Select -L 1
Get-Content -Path '.\mytext.txt' -TotalCount 3 | Select-Object -Last 1从CMD控制台(命令提示符):(只获得第7行)
PowerShell "GC .\mytext.txt -T 7|Select -L 1"
PowerShell -NoProfile "Get-Content -Path '.\mytext.txt' -TotalCount 7 | Select-Object -Last 1"要获得第3行到第7行,请执行以下操作:
$FirstLine = 3
$LastLine=7
powershell -NoProfile -Command "Get-Content -Path '.\t.txt' -TotalCount $LastLine | Select-Object -Last ($LastLine - $FirstLine + 1)"或者,在cmd.exe批处理脚本中。
SET "FIRSTLINE=3"
SET "LASTLINE=7"
powershell -NoProfile -Command ^
"Get-Content -Path '.\t.txt' -TotalCount %LASTLINE% |" ^
"Select-Object -Last (%LASTLINE% - %FIRSTLINE% + 1)"发布于 2018-05-23 14:52:47
@echo off
setlocal
set "FILE_TO_PROCESS=%~f1"
set /a LINE_NUMBER=%~2
set /a trim=LINE_NUMBER-1
break>"%temp%\empty"&&fc "%temp%\empty" "%FILE_TO_PROCESS%" /lb %LINE_NUMBER% /t |more +4 | findstr /B /E /V "*****"|more +%trim%
endlocal尝试使用这个bat (称为lineNumber.bat),第一个参数是要处理的文件,第二个参数是行号:
call lineNumber.bat someFile.txt 5发布于 2018-05-23 15:25:18
有几种方法可以做到这一点。您的第一个选项是正常地遍历for循环,并在到达所需的行后退出循环。
@echo off
:: Specify which line to return
set get_line=7
:: Skip all lines before it, then print the next line and abort
set /a get_line-=1
for /F "skip=%get_line% delims=" %%A in (mytext.txt) do (
echo %%A
goto :end_loop
)
:end_loop另一个选项是将不需要的行之前的所有行存储在temp变量中,然后显示下一行。
@echo off
setlocal enabledelayedexpansion
:: Specify which line to return
set get_line=7
:: Skip all lines before it, then print the next line and abort
set /a get_line-=2
(
for /L %%A in (0,1,%get_line%) do set /p skip_line=
set /p return_line=
) <file.txt
echo !return_line!注意,第一个选项不适合返回脚本的第一行。
https://stackoverflow.com/questions/50491373
复制相似问题