UFO ET IT

Windows 배치 파일에서 부분 문자열을 바꾸는 방법

ufoet 2020. 11. 10. 22:40
반응형

Windows 배치 파일에서 부분 문자열을 바꾸는 방법


누구든지 Windows에서 배치 파일을 사용하여 나에게 말할 수 있습니까? 파일에서 읽고 string = bathfrom file contains = bath Abath Bbath XYZbathABC를 string으로 바꾸는 방법 hello은 다음과 같습니다.hello Ahello Bhello XYZhelloABC


Andriy M 에서 확장 하면 여러 줄이있는 파일에서도 파일에서이 작업을 수행 할 수 있습니다.

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "INTEXTFILE=test.txt"
set "OUTTEXTFILE=test_out.txt"
set "SEARCHTEXT=bath"
set "REPLACETEXT=hello"

for /f "delims=" %%A in ('type "%INTEXTFILE%"') do (
    set "string=%%A"
    set "modified=!string:%SEARCHTEXT%=%REPLACETEXT%!"
    echo !modified!>>"%OUTTEXTFILE%"
)

del "%INTEXTFILE%"
rename "%OUTTEXTFILE%" "%INTEXTFILE%"
endlocal

편집하다

감사합니다 David Nelson, 스크립트를 업데이트하여 더 이상 하드 코딩 된 값이 없습니다.


SET string=bath Abath Bbath XYZbathABC
SET modified=%string:bath=hello%
ECHO %string%
ECHO %modified%

편집하다

처음에는 파일에서 문자열을 읽어서 대체하기를 원한다는 것을 처음에는 보지 못했습니다.

글쎄, 배치 파일을 사용하면 파일 작업 기능이별로 없습니다. 이 특별한 경우에는 한 줄을 읽고 교체를 수행 한 다음 수정 된 줄을 출력해야합니다. 모든 파일에서 'bath'의 모든 발생을 교체해야하는 경우 루프를 사용해야합니다.

@ECHO OFF
SETLOCAL DISABLEDELAYEDEXPANSION
FOR /F %%L IN (file.txt) DO (
  SET "line=%%L"
  SETLOCAL ENABLEDELAYEDEXPANSION
  ECHO !line:bath=hello!
  ENDLOCAL
)
ENDLOCAL

파일에 리디렉션을 추가 할 수 있습니다.

  ECHO !line:bath=hello!>>file2.txt

또는 배치 파일에 리디렉션을 적용 할 수 있습니다. 그것은 있어야 다른 파일 수.

2 편집

!, ^et al 과 같은 배치 스크립트 구문에서 특별한 의미를 갖는 일부 문자의 올바른 처리를 위해 지연된 확장의 적절한 토글을 추가했습니다 . (고마워, jeb !)


빈 줄 건너 뛰기를 방지하기 위해 (conf 파일에서 가독성을 제공) aflat 및 jeb answer ( here )를 다음과 같이 결합합니다.

@echo off
setlocal enabledelayedexpansion
set INTEXTFILE=test.txt
set OUTTEXTFILE=test_out.txt
set SEARCHTEXT=bath
set REPLACETEXT=hello
set OUTPUTLINE=

for /f "tokens=1,* delims=¶" %%A in ( '"findstr /n ^^ %INTEXTFILE%"') do (
   SET string=%%A
   for /f "delims=: tokens=1,*" %%a in ("!string!") do set "string=%%b"
   if  "!string!" == "" (
       echo.>>%OUTTEXTFILE%
   ) else (
      SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
      echo !modified! >> %OUTTEXTFILE%
  )
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%

To avoid problems with the batch parser (e.g. exclamation point), look at Problem with search and replace batch file.

Following modification of aflat's script will include special characters like exclamation points.

@echo off
setlocal DisableDelayedExpansion
set INTEXTFILE=test.txt
set OUTTEXTFILE=test_out.txt
set SEARCHTEXT=bath
set REPLACETEXT=hello
set OUTPUTLINE=

for /f "tokens=1,* delims=¶" %%A in ( '"type %INTEXTFILE%"') do (
    SET string=%%A
    setlocal EnableDelayedExpansion
    SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!

    >> %OUTTEXTFILE% echo(!modified!
    endlocal
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%

To avoid blank line skipping just replace this:

echo !modified! >> %OUTTEXTFILE%

with this:

echo.!modified! >> %OUTTEXTFILE%

If you have Ruby for Windows,

C:\>more file
bath Abath Bbath XYZbathABC

C:\>ruby -pne "$_.gsub!(/bath/,\"hello\")" file
hello Ahello Bhello XYZhelloABC

참고URL : https://stackoverflow.com/questions/5273937/how-to-replace-substrings-in-windows-batch-file

반응형