如何解决批处理文件从变量中未知数量的定界符中提取某些标记
第三次,希望是最后的问题改进...
批处理文件将文本文件逐行读入for循环到变量中。所述文本文件的每一行都可以与下一行完全格式化。唯一常见的定界符是每行某处的四位数字(年份)。目的是通过回显为每行返回上述四位数字之后的任何文本。
文本文件示例:
Monday,January 1,1900 there was an event-e6718
On this day in 1904 nothing occurred
Wednesday,March 3,1908 an error occurred when attempting to access the log
Thursday,1911 - access denied
Friday,in whatever month,on whatever day,in 1938,nothing happened
因此,根据上述文本文件示例,返回结果将类似于...
there was an event-e6718
nothing occurred
an error occurred when attempting to access the log
- access denied
nothing happened
从1318 PST开始,我已经尝试了以下注释中的每个代码段,但没有一个能够返回我需要返回的数据。
但是,这些评论与我最初提出的问题有关,此问题已得到明显改善。
我什至尝试过正则表达式“ ^ [1-9] [0-9] [0-9] [0-9] $”,但是我是新手正则表达式,所以我确定我错了。
这有可能吗?
谢谢。
解决方法
批量生产是一项可怕的任务。 REGEX是一个很好的工具,但是cmd
不支持它(除了findstr
严重受损的子集之外)。如果您愿意使用external tool,则变得很容易:
<old.txt call jrepl ".*(\d{4})\D\ *(.*$)" "$2" >new.txt
搜索一个四位数的数字\d{4}
,后跟一个非数字的\D
和零个或多个空格,然后取其余的字符直到“ EndOfLine” .*$
。 (括号)标记匹配,由$x
引用。您所需的字符串在$2
中。
输出示例文件:
there was an event-e6718
nothing occurred
an error occurred when attempting to access the log
- access denied
there was an event-dsfd318
nothing happened
如果您决定包括年份,则可以在$1
中找到它:
<old.txt call jrepl ".*(\d{4})\D\ *(.*$)" "$1: $2" >new.txt
给予:
1900: there was an event-e6718
1904: nothing occurred
1908: an error occurred when attempting to access the log
1911: - access denied
1910: there was an event-dsfd318
1938: nothing happened
call
是批处理文件所必需的,因为jrepl
是批处理文件,因此如果没有call
就不会返回。
(REGEX模式可能有待改进;我对此没有太多经验。)
jrepl.bat
由dbenham编程。
如果字符串中确实没有公共点或令牌计数没有一致性,请在下面的for循环中调整迭代次数以匹配可能的最大令牌。
@Echo off & Setlocal EnableDelayedexpansion
Set "event=Monday,January 1,1900 there was an event-e6718"
For /L %%i in (1 1 10) Do (
Set "event=!event:*,=!"
)& rem // arbitrary number of iterations that should be adjusted to match the maximum expected tokens
Set "event=%event:~5,100%"& rem // remove the year[space] from the string - final string maximum length is also arbitrary and may need adjusting.
Echo/%event%
**更新** 使用上述方法的宏版本来获取for循环中最后一个令牌的宏示例:
注意:您需要调整输入文件的文件路径。
@Echo off
(Set \n=^^^
%=Newline Var=%
)
Set Gettoken=For %%n in (1 2) Do if %%n==2 (%\n%
For /F "Delims=" %%G in ("!string!") Do (%\n%
Set "output=%%G"%\n%
For %%D in ("!Delim!") Do For /L %%i in (1 1 10) Do Set "output=!output:*%%~D=!"%\n%
Set "output=!output:~5,100!"%\n%
)%\n%
Set output%\n%
) Else Set string=
Setlocal EnableDelayedExpansion
Set "Delim=,"&& For /F "Delims=" %%I in (inputfile.txt) Do %GetToken%%%I
编辑:
单批次解决方案,以解决实际问题。
@Echo off & CD "%~dp0"
Setlocal Enabledelayedexpansion
rem // replace inputfile.txt with your filepath
For /F "Delims=" %%L in (inputfile.txt) Do (
Call :sub "%%L"
rem // the below for loop will remove everything up to including the first year from the string
rem // as well as traling coma[space] / [space]
For %%E in (!Errorlevel!) Do (
If Not "%%E"=="0" (
Set "String=!String:*%%E=####!"
Set "String=!String:####,=!"
Set "String=!String:#### =!"
Set "String=!String:####=!"
)
)
rem // output only if a year "delimiter" was encountered
If not "%%~L"=="!String!" Echo/!String!
)
Exit /b
:sub
Set "String=%~1"
rem // adjust for loop %%I for valid year range and %%# for maximum expected string length
For /L %%I in (1899 1 2050) Do (For /L %%# in (0 1 100) Do (If "!String:~%%#,4!"=="%%I" (Exit /B %%I)))
Exit /B 0
,
尝试一下:
from selenium.webdriver.common.keys import Keys
...
driver.find_element_by_id('okta-signin-username').send_keys(USERNAME)
password_field = driver.find_element_by_id('okta-signin-password')
password_field.send_keys(PASSWORD)
password_field.send_keys(Keys.RETURN)
textfile.txt
@echo off & setlocal enabledelayedexpansion
for /f "delims=" %%i in ('type "C:\textfile.txt" ^| findstr /IRC:"there was an event"') do (
set "event=%%i"
echo "!event:*there was an event=there was an event!"
)
结果:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。