For 루프 및 find 명령

For 루프 및 find 명령

find 명령의 행 번호만 파싱하고 싶습니다. 내 시도는 다음과 같습니다.

FOR /f "tokens=1 delims=[" %a IN ('find /n "string" 2.txt') DO @ECHO %a

하지만 sth를 반환합니다. 그렇게:

---------- 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


추가 리소스:

관련 정보