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

instead of just:

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

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

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

as expected.

1 Answer 1

Search for a text string in a file & display all the lines where it is found.

      FIND [/V] [/C] [/N] [/I] "string" [pathname(s)]

   "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

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

Additional resources:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .