[英]Get a string in a line with a string pattern (CMD) - Windows Batch
我正在使用 windows 命令提示符 (cmd)。
我有这个文件“myfile.txt”,我想在每一行中获取与我的模式匹配的字符串。
例如,如果我在“myfile.txt”中有这个:
The file alpha_123.txt has been created
The file gama_234.txt has been created
The new file alpha_789.txt has been created
如果我搜索“alpha”,结果应该是这样的:
alpha_123.txt
alpha_789.txt
现在我有这个代码: findstr /c "alpha" myfile.txt
但它返回整行,但我只想要特定的字符串。
提前致谢!
假设您在正确的目录中,这样的事情应该有效。
for /f "tokens=3 delims= " %G in ('findstr "alpha" MyFile.txt') do @echo %G
或者是这样的:
for /f "tokens=3 delims= " %G in ('findstr /r "\<alpha_.*txt" MyFile.txt') do @echo %G
这可以在windows
上的cmd
command-prompt
下使用。
powershell -nop -c ((sls 'alpha.*?\s' myfile.txt).Matches.Value).Trim()
如果放入cmd
batch-file
,则不应使用别名。
powershell -NoLogo -NoProfile -Command ^
((Select-String -Pattern 'alpha.*?\s' -Path 'myfile.txt').Matches.Value).Trim()
非常简单:获取每一行,写下每个单词(标记)并过滤所需的字符串:
@echo off
setlocal
set "search=alpha"
for /f "delims=" %%a in (t.txt) do (
for %%b in (%%a) do (
echo %%b|find "%search%"
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.