windows - 使用 for 循环删除几个模式后如何从命令输出中提取动态模式
问题描述
使用第一个“for”循环从输出中提取特定行,同时希望使用一定数量的文本进一步删除提取的输出,请您帮忙。
for /f "tokens=* delims= " %i in ('dir /r ^| findstr -li "zone.id"') do @echo %i >> C:\zone.txt
上述循环的输出如下:
26 789A44F9D1497937126FF305C6CB89D0.ics:Zone.Identifier:$DATA
29 active.csv:Zone.Identifier:$DATA
26 file trans today.jpg:Zone.Identifier:$DATA
26 BC of raji.pdf:Zone.Identifier:$DATA
29 modern - Contest 2016 Submission_updated 2019 - july-10.pdf:Zone.Identifier:$DATA
26 mesge_mon_results (1).csv:Zone.Identifier:$DATA
26 [MS-DoCTS].pdf:Zone.Identifier:$DATA
我想删除 26 / 29 之前和之后的空格,包括 2 位数字和 :$DATA 并获得如下输出:
789A44F9D1497937126FF305C6CB89D0.ics:Zone.Identifier
active.csv:Zone.Identifier
file trans today.jpg:Zone.Identifier
BC of raji.pdf:Zone.Identifier
modern - Contest 2016 Submission_updated 2019 - july-10:Zone.Identifier
mesge_mon_results (1).csv:Zone.Identifier
[MS-DoCTS].pdf:Zone.Identifier
尽管可以使用记事本中的替换选项轻松完成,但我喜欢通过批处理脚本自动执行此操作。
解决方案
你应该在 vbscript 中使用 Regex 和这样的批处理文件:你可以在这里看到 Regex Demo
@echo off
Mode 85,20 & color 0A
Title Replace String using Regex with vbscript
Set "InputFile=%~dp0zone.txt"
Set "OutPutFile=%~dp0New_Zone_After_Replacing.txt"
echo(
:: To show Results on screen of console
Call :Search_Replace "%InputFile%" CON
:: To write Result in new file
Call :Search_Replace "%InputFile%" "%OutPutFile%"
echo(
echo Press any key to show the results in a new file :
echo "%OutPutFile%"
pause>nul
Start "" "%OutPutFile%"
echo(
echo Did you want to update and replace all in your original file "%InputFile%" ?
Pause>nul
Move /Y "%OutPutFile%" "%InputFile%">nul
Start "" "%InputFile%" & Exit
::-----------------------------------------------------------------------------------
:Search_Replace <InputFile> <OutPutFile>
(
echo WScript.StdOut.WriteLine Search_Replace(Data^)
echo Function Search_Replace(Data^)
echo Dim strPattern, strReplace, strResult,oRegExp
echo Data = "%~1"
echo Data = WScript.StdIn.ReadAll
echo strPattern = "\b(\d\d\s)(.*)(:\$DATA)"
echo strReplace = "$2"
echo Set oRegExp = New RegExp
echo oRegExp.Global = True
echo oRegExp.IgnoreCase = True
echo oRegExp.Pattern = strPattern
echo strResult = oRegExp.Replace(Data,strReplace^)
echo Search_Replace = strResult
echo End Function
)>"%tmp%\%~n0.vbs"
cscript //nologo "%tmp%\%~n0.vbs" < "%~1" > "%~2"
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
Exit /B
::----------------------------------------------------------------------------------
编辑:在批处理文件中使用正则表达式和 Powershell:
@echo off
Title Search and Replace with Powershell and batch
Set "InputFile=%~dp0zone.txt"
Set "OutPutFile=%~dp0New_zone.txt"
Call :Search_Replace "%InputFile%" "%OutPutFile%"
If Exist "%OutPutFile%" Start "" "%OutPutFile%"
Exit
REM -------------------------------------------------------------------------------------
:Search_Replace <InputFile> <OutPutFile>
Powershell ^
GC -path "%~1" ^| %% { $_ -Replace '\b(\d\d\s^)(.*^)(:\$DATA^)','$2' } ^| Out-File "%~2"
Exit /B
REM -------------------------------------------------------------------------------------
推荐阅读
- multithreading - 在后台队列上创建 NSAttributedString:“[NSCell init] 必须仅在主线程中使用”
- javascript - 为什么我调用 this.form.submit() 后我的选择没有改变?
- r - R在检查字母顺序的列中交换值
- node.js - Heroku 部署后运行 URL 显示应用程序错误
- java - 如何跟踪 Firestore 读/写操作?
- git - VisualStudio 在 Rebase 时会创建非常不必要的合并冲突?
- c# - Azure Blob 存储的文件大小限制?
- kubernetes - 我可以在没有 virtualBox 的情况下在 ubuntu 上安装 minikube 吗?
- url - HAProxy http-request 将特定路径重定向到另一个路径
- android - Retrofit2:保护baseUrl