
我只想解析 find 指令的行號。這是我的嘗試:
FOR /f "tokens=1 delims=[" %a IN ('find /n "string" 2.txt') DO @ECHO %a
但它返回某物。像那樣:
---------- 2.TXT
2]string
而不僅僅是:
---------- 2.TXT
當我將分隔符號更改為 ] - 沒關係 - %a 包含:
---------- 2.TXT
[2
正如預期的那樣。
答案1
FIND
Search for a text string in a file & display all the lines where it is found.
Syntax
FIND [/V] [/C] [/N] [/I] "string" [pathname(s)]
Key
"string" The text string to find (must be in quotes).
[pathname] A drive/file(s) to search (wildcards accepted).
⇉ /V Display all lines NOT containing the specified string.
/C Count the number of lines containing the string.
/N Display Line numbers.
/I Ignore the case of characters when searching for the string.
[/off[line]] Do not skip files that have the offline attribute set.
1.您不需要將分隔符號定義為"["
或者"]"
> for /f delims^= %a IN ('find/n "string" 2.txt^|find/v "["')do @echo=%a
觀察:在以下位置使用一個空格:delims^=ESPACE%a
2.只需添加一個^|find/v "["
, 省略包含以下內容的行[n]
在輸出中
> for /f delims^= %a IN ('find/n "string" 2.txt^|find/v "["')do @echo=%a
---------- 2.TXT
- 僅取得檔案名稱:
> for /f tokens^=1*delims^=-^ %a IN ('find/n "string" 2.txt^|find/v "["')do @echo=%a
2.TXT
觀察:在以下位置使用兩個空格:delims^=-^ESPACEESPACE%a
其他資源: