배치를 사용하여 txt 파일에서 여러 공백을 하나의 공백으로 바꿉니다.

배치를 사용하여 txt 파일에서 여러 공백을 하나의 공백으로 바꿉니다.

여기에 내가 얻은 것이 있습니다

    @ECHO OFF
   SETLOCAL
   ::
   :: delete first line/lines
   :: and replace each occurrence of 2 or more spaces
   :: by a delimiter
   ::
   DEL outfile.txt   2>nul /F /Q
   :: replace with ->
   SET delim= 
   :: set number of lines to delete 
   FOR /f "skip=1delims=" %%i IN (Text.txt) DO (
   SET line=%%i
   (SET newline=)
   SET count=0
   CALL :change
   )
   GOTO :eof

   :CHANGE
   SET c1=%line:~0,1%
   SET line=%line:~1%
   IF "%c1%"==" " (SET /a count+=1) ELSE (
   IF %count%==0 (SET newline=%newline%%c1%) ELSE (
   IF %count%==1 (SET newline=%newline% %c1%) ELSE (
   SET newline=%newline%%delim%%c1%)
   SET count=0
   )
   )
   IF DEFINED line GOTO CHANGE
   ::
   :: You may want to preserve trailing spaces
   :: or convert them...
   ::
   IF %count%==0 GOTO print
   IF %count%==1 SET newline=%newline% &GOTO print
   SET newline=%newline%%delim%
   :PRINT
   >>outfile.txt ECHO %newline%
   GOTO :eof

다음은 파일의 예입니다.

    P1 something             232
2      232  233
10     232  232
2312   232  232
231    232 323

더 이상 첫 번째 줄을 삭제할 필요가 없습니다. 첫 번째 줄/줄을 건너뛰는 공백을 바꾸는 방법을 알아내는 데 도움이 될 수 있습니까?

답변1

11번째 줄에서 다음을 바꿉니다:

FOR /f "skip=1delims=" %%i IN (Text.txt) DO (

~와 함께

FOR /f "delims=" %%i IN (Text.txt) DO (

공백이 포함된 파일 이름에 따옴표 사용을 허용하려면,이 StackOverflow 답변에 따라, 다음 옵션을 사용할 수 있습니다 usebackq.

FOR /f "usebackq delims=" %%i IN ("text 2.txt") DO (

관련 정보