0

I'd like to parse out only the line number of find command. Here is my attempt:

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

but it returns sth. like that:

---------- 2.TXT
2]string

instead of just:

---------- 2.TXT

When i change the delimiter to ] -it's ok - the %a contains:

---------- 2.TXT
[2

as expected.

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. You do not need to define the delimiter as "[" or "]"

> for /f delims^= %a IN ('find/n "string" 2.txt^|find/v "["')do @echo=%a

Obs.: Use one space in: delims^=ESPACE%a

2. Just add a ^|find/v "[", to omit lines containing [n] in the output

> for /f delims^= %a IN ('find/n "string" 2.txt^|find/v "["')do @echo=%a
---------- 2.TXT
  • To get only the file name:
> for /f tokens^=1*delims^=-^  %a IN ('find/n "string" 2.txt^|find/v "["')do @echo=%a
2.TXT

Obs.: Use two spaces in: delims^=-^ESPACEESPACE%a


Additional resources:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.