批处理文件从变量中未知数量的定界符中提取某些标记

如何解决批处理文件从变量中未知数量的定界符中提取某些标记

第三次,希望是最后的问题改进...

批处理文件将文本文件逐行读入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.batdbenham编程。

,

如果字符串中确实没有公共点或令牌计数没有一致性,请在下面的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!"
)

结果:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-