如何通过批处理脚本从TXT文件的最后一行提取最后一个词
How to extract the last word from the last line of a TXT file through a batch script
我正在尝试从 txt 文件的最后一行提取最后一个词。
我要的结果就是Cup$2!.
这是我试过的:
@echo off
SetLocal EnableDelayedExpansion
set L=1
for /F "tokens=2 delims=" %%a in (corner.txt) do (
set line=%%a
if !L!==7 set Line7=%%a
set /a L=!L!+1
)
echo The word is %Line7%
pause
我得到的结果是单词是。
我应该编辑什么以获得上述结果?
解析批处理文件中的字符串从来都不理想,但这似乎可行:
@echo off
setlocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
echo.line 1 > "%temp%\SO_Test.txt"
echo. >> "%temp%\SO_Test.txt"
echo.foo^&bar!baz hello/world Cup^&2!/Cup!>> "%temp%\SO_Test.txt"
goto start
:start
set "line="
for /F "usebackq delims=" %%a in ("%temp%\SO_Test.txt") do @set line=%%a
:word
set "b="
rem Delims is slash, tab, space and the order is important
for /F "tokens=1,* delims=/ " %%A in ("%line%") do (
set "line=%%A"
set "b=%%B"
)
if not "%b%" == "" (
set "line=%b%"
goto word
)
echo.Last word is "%line%"
这是一个简单的示例,说明如何通过阅读代码捕获所需的子字符串,即第七行的最后一个子字符串:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "var="
For /F UseBackQ^ Skip^=7^ Delims^=^ EOL^= %%G In ("corner.txt") Do Set "var=%%~nxG" & GoTo Next
If Not Defined var GoTo EndIt
:Next
SetLocal EnableDelayedExpansion
Echo The word is !var!
EndLocal
:EndIt
Echo Press any key to close . . .
Pause 1>NUL
EndLocal
Exit /B
但是,如果正如您的问题标题和 body 所要求的那样,您想要最后一行的最后一个子字符串,则稍微简单一些:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "var="
For /F UseBackQ^ Delims^=^ EOL^= %%G In ("corner.txt") Do Set "var=%%~nxG"
If Not Defined var GoTo EndIt
SetLocal EnableDelayedExpansion
Echo The word is !var!
EndLocal
:EndIt
Echo Press any key to close . . .
Pause 1>NUL
EndLocal
Exit /B
但是我不会解释任何内容,请使用页面顶部的搜索工具,以及我使用过的每个命令的内置帮助的输出。
获取行数。
for /f "tokens=3*" %%i in ('find /c /v /n /i"" corner.txt') do set /a v=%%i-1
然后从最后一行的第 7 个单词获取最后的值:
for /f "tokens=7*" %%a in ('more corner.txt +%v%') do set "String="%%b""
变量 %String%
将值用双引号括起来:Cup&2!/Cup!
如果您使用 /
作为分隔符,您可以获得最后一个值:
for /f "delims=/ tokens=2*" %%a in (%String%) do @echo %%a
您可以在 batch-file
运行 by cmd
在 windows
上使用它。
FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command ^
"((Get-Content -Path 'corner.txt' -Last 1) -split '[\s*/]')[-1]"') DO (SET "LINE7=%%A")
ECHO %LINE7%
如果脚本是用 PowerShell 而不是 cmd 语言编写的,它可能是一行。
$Line7 = ((Get-Content -Path 'corner.txt' -Last 1) -split '[\s*/]')[-1]
@ECHO OFF
SETLOCAL
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q70761955.txt"
SET "amper=&"
FOR /f "usebackq delims=" %%b IN ("%filename1%") DO SET "lastword=%%b"
:: Replace ampersands with spaces, then slashes with spaces
CALL SET "lastword=%%lastword:%amper%= %%"
SET "lastword=%lastword:/= %"
FOR %%b IN (%lastword%) DO SET "lastword=%%b"
ECHO last "word" is -^>%lastword%^<-
GOTO :EOF
读取文件的每一行,保留lastword
中最后读取的行。
替换每个“分隔符”字符(&
和 /
隐含在问题中。其他 - 无信息。)
Select最后一行的最后一个“字”。
您需要更改分配给 sourcedir
的值以适合您的情况。该列表使用适合我的系统的设置。
我特意在名称中包含空格以确保正确处理空格。
我使用了一个名为 q70761955.txt
的文件,其中包含您的数据用于我的测试。
我正在尝试从 txt 文件的最后一行提取最后一个词。
我要的结果就是Cup$2!.
这是我试过的:
@echo off
SetLocal EnableDelayedExpansion
set L=1
for /F "tokens=2 delims=" %%a in (corner.txt) do (
set line=%%a
if !L!==7 set Line7=%%a
set /a L=!L!+1
)
echo The word is %Line7%
pause
我得到的结果是单词是。
我应该编辑什么以获得上述结果?
解析批处理文件中的字符串从来都不理想,但这似乎可行:
@echo off
setlocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
echo.line 1 > "%temp%\SO_Test.txt"
echo. >> "%temp%\SO_Test.txt"
echo.foo^&bar!baz hello/world Cup^&2!/Cup!>> "%temp%\SO_Test.txt"
goto start
:start
set "line="
for /F "usebackq delims=" %%a in ("%temp%\SO_Test.txt") do @set line=%%a
:word
set "b="
rem Delims is slash, tab, space and the order is important
for /F "tokens=1,* delims=/ " %%A in ("%line%") do (
set "line=%%A"
set "b=%%B"
)
if not "%b%" == "" (
set "line=%b%"
goto word
)
echo.Last word is "%line%"
这是一个简单的示例,说明如何通过阅读代码捕获所需的子字符串,即第七行的最后一个子字符串:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "var="
For /F UseBackQ^ Skip^=7^ Delims^=^ EOL^= %%G In ("corner.txt") Do Set "var=%%~nxG" & GoTo Next
If Not Defined var GoTo EndIt
:Next
SetLocal EnableDelayedExpansion
Echo The word is !var!
EndLocal
:EndIt
Echo Press any key to close . . .
Pause 1>NUL
EndLocal
Exit /B
但是,如果正如您的问题标题和 body 所要求的那样,您想要最后一行的最后一个子字符串,则稍微简单一些:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "var="
For /F UseBackQ^ Delims^=^ EOL^= %%G In ("corner.txt") Do Set "var=%%~nxG"
If Not Defined var GoTo EndIt
SetLocal EnableDelayedExpansion
Echo The word is !var!
EndLocal
:EndIt
Echo Press any key to close . . .
Pause 1>NUL
EndLocal
Exit /B
但是我不会解释任何内容,请使用页面顶部的搜索工具,以及我使用过的每个命令的内置帮助的输出。
获取行数。
for /f "tokens=3*" %%i in ('find /c /v /n /i"" corner.txt') do set /a v=%%i-1
然后从最后一行的第 7 个单词获取最后的值:
for /f "tokens=7*" %%a in ('more corner.txt +%v%') do set "String="%%b""
变量 %String%
将值用双引号括起来:Cup&2!/Cup!
如果您使用 /
作为分隔符,您可以获得最后一个值:
for /f "delims=/ tokens=2*" %%a in (%String%) do @echo %%a
您可以在 batch-file
运行 by cmd
在 windows
上使用它。
FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command ^
"((Get-Content -Path 'corner.txt' -Last 1) -split '[\s*/]')[-1]"') DO (SET "LINE7=%%A")
ECHO %LINE7%
如果脚本是用 PowerShell 而不是 cmd 语言编写的,它可能是一行。
$Line7 = ((Get-Content -Path 'corner.txt' -Last 1) -split '[\s*/]')[-1]
@ECHO OFF
SETLOCAL
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q70761955.txt"
SET "amper=&"
FOR /f "usebackq delims=" %%b IN ("%filename1%") DO SET "lastword=%%b"
:: Replace ampersands with spaces, then slashes with spaces
CALL SET "lastword=%%lastword:%amper%= %%"
SET "lastword=%lastword:/= %"
FOR %%b IN (%lastword%) DO SET "lastword=%%b"
ECHO last "word" is -^>%lastword%^<-
GOTO :EOF
读取文件的每一行,保留lastword
中最后读取的行。
替换每个“分隔符”字符(&
和 /
隐含在问题中。其他 - 无信息。)
Select最后一行的最后一个“字”。
您需要更改分配给 sourcedir
的值以适合您的情况。该列表使用适合我的系统的设置。
我特意在名称中包含空格以确保正确处理空格。
我使用了一个名为 q70761955.txt
的文件,其中包含您的数据用于我的测试。