簡體   English   中英

獲取具有字符串模式的行中的字符串 (CMD) - Windows 批處理

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM