
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
観察:以下の場合はスペースを 1 つ使用します: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
観察:以下の場合にはスペースを 2 つ使用します。delims^=-^ESPACEESPACE%a
追加リソース: