Secuencia de comandos CMD para buscar y reemplazar una parte de la línea encontrada en el archivo

I need to find all lines in myfile.txt containing word 'MyWord', and then replace a part of this string next way:

Línea original:

 ...,31-01-2012,00,some_words_and_symbols_and_digits,MyWord,...

After replace:

 ...,31-01-2012,01,some_words_and_symbols_and_digits,MyWord,...

Please, help me to write this cmd script!

OK.. I have next code:

@echo off
set code=MyWord
set req=new request

FOR /F "usebackq delims=, tokens=1,2,3,4,5,6,7,8,9*" %%a in (MyFile.txt) do (

IF %%h==%code% (
    SET tempstr=%%a,%%b,%%c,%%d,60,%%f,%%g,%%h,%%i
    ) ELSE (
    SET tempstr=%%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i
    ) 

IF %%a==%req% (
    SET echo %%a >> new.strings
    ) ELSE (
    echo %tempstr% >> new.strings
    )

)
@echo on

And I have in my file something like:

 new request
 ...,31-01-2012,01,some_words_and_symbols_and_digits,MyWord,...
 new request
 ...,30-11-2011,01,some_words_and_symbols_and_digits,OtherWords,...

But then I have error:

ELSE was unexpected at this time.

And If I'm trying simple next in the end

IF %%a==%req% SET tempstr=%%a
echo %tempstr% >> new.strings

Then I have only one last row instead of other else

preguntado el 31 de enero de 12 a las 08:01

I edited my answer in response to your edit. Next time please leave a comment as a notification as stackoverflow does not inform users about edits to questions they answered to. -

1 Respuestas

Puede usar el find command to filter the lines containing given text. As I see, the file is CSV. So you can use for /f to parse the lines found. Then you can echo all parsed files replacing the field you want.

This will replace all values in the 3rd column with "01"

@echo off
for /f "usebackq delims=, tokens=1,2,3,4,<put as many as you need>" %%A in (`find "MyWord" myfile.txt`) do echo %%A,%%B,01,%%D,<as many %%letters as tokens>

If you want to replace the value only on some lines, you can use if comando dentro for /f lazo.

==== EDIT

The problem is with the value of req variable. It contains a space, so after substitution your second if statement has the following form:

IF %%a==new request (

Así que si %%a es igual a new se ejecutará request ( echo ...... ) and then ELSE is unexpected indeed. Enclose both %%a y la %req% in quotation marks and the problem will disappear.

But I see also other problems. First, you have redundant set in your second if statement. Second, you need to use delayed expansion of variables, or your echo %tempstr% no funcionará

Your code after needed changes:

@Echo off
setlocal enabledelayedexpansion

set code=MyWord
set req=new request

FOR /F "usebackq delims=, tokens=1,2,3,4,5,6,7,8,9*" %%a in (MyFile.txt) do (

  IF %%h==%code% (
    SET tempstr=%%a,%%b,%%c,%%d,60,%%f,%%g,%%h,%%i
  ) ELSE (
    SET tempstr=%%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i
  ) 

  IF "%%a"=="%req%" (
    echo %%a >> new.strings
  ) ELSE (
    echo !tempstr! >> new.strings
  )
)

endlocal

Respondido 01 Feb 12, 00:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.