Para bucle y comando de búsqueda

Para bucle y comando de búsqueda

Me gustaría analizar solo el número de línea del comando de búsqueda. Aquí está mi intento:

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

pero devuelve algo. como eso:

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

en lugar de simplemente:

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

Cuando cambio el delimitador a], está bien, %a contiene:

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

como se esperaba.

Respuesta1

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.No es necesario definir el delimitador como"["o"]"

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

Obs.:Utilice un espacio en:delims^=ESPACE%a

2.Sólo agrega un^|find/v "[", para omitir líneas que contengan[n]en la salida

> for /f delims^= %a IN ('find/n "string" 2.txt^|find/v "["')do @echo=%a
---------- 2.TXT
  • Para obtener solo el nombre del archivo:
> for /f tokens^=1*delims^=-^  %a IN ('find/n "string" 2.txt^|find/v "["')do @echo=%a
2.TXT

Obs.:Utilice dos espacios en:delims^=-^ESPACEESPACE%a


Recursos adicionales:

información relacionada