1

I need to replace, using a Batch file (.bat), a blank character, placed always in a fixed position (column 13) of each line in a .txt file, with another fixed character. What kind of function can I use?

Example of my file:

1000588141025 00LEOTOURING SRL   VIA FILADELFO CASTRO,1         LENTINI 

I'd like to have:

1000588141025A00LEOTOURING SRL   VIA FILADELFO CASTRO,1         LENTINI 
DanieleArm
  • 11
  • 3

2 Answers2

0
@ECHO OFF
:: version 1 - "replace regardless"
SETLOCAL ENABLEDELAYEDEXPANSION
(
 FOR /f "delims=" %%a IN (q26733113.txt) DO (
  SET "line=%%a"
  ECHO(!line:~0,13!A!line:~14!
 )
)>newfile.txt

:: version2 - "replace only if space found in position"
SETLOCAL ENABLEDELAYEDEXPANSION
(
 FOR /f "delims=" %%a IN (q26733113.txt) DO (
  SET "line=%%a"
  IF "!line:~13,1!"==" " (ECHO(!line:~0,13!A!line:~14!) ELSE (ECHO(%%a)
 )
)>newfile2.txt

GOTO :EOF

I used a file named q26733113.txt containing your data for my testing. Produces newfile.txt and newfile2.txt - the first changing column - well 14 in normal counting, as indicated in your test data - regardless of the content of column 14. The second only replaces column 14 with the A if it was a space, otherwise leaves it as-is.

Don't worry about the seeming-unbalanced parentheses. echo( has special properties - te parenthesis isn't counted...

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

Using REPL.BAT:

type "file.txt"|repl "^(.{12}) " "$1A" >"file.txt.new"
move /y "file.txt.new" "file.txt" >nul
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390