pretyv5
pretyv5

Reputation: 105

remove spaces from text file and adding character after specific text

I have a text file where I need to add '1' after the character and removing any spaces between the first two columns. I tried to use the below query but was getting this error

201N001466  AD55JGU0604140     VOLKSWAGEN          GOLF S                   BLACK        
201N001437  AF14HFY0604140     BMW                 520D SE AUTO             GREY  
201N001298  AF51NSN0604140     SKODA               FABIA COMFORT 8V

this is the format i am looking to add '1' after N and remove any spaces between the first 2 columns.

201N1001466AD55JGU0604140     VOLKSWAGEN          GOLF S                   BLACK         
201N1001437AF14HFY0604140     BMW                 520D SE AUTO             GREY               
201N1001298AF51NSN0604140     SKODA               FABIA COMFORT 8V     

this was my query

@echo off
  if [%1]==[] then echo File name missing & goto :EOF
  (for /f "tokens=1" %%a in (%1) do echo %%a) > %~n1_out.txt

Upvotes: 0

Views: 73

Answers (3)

Stephan
Stephan

Reputation: 56180

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2,*" %%a in (infile.txt) do (
  set a=%%a
  echo !a:~0,4!1!a:~4!%%b %%c
) >>outfile.txt

Explanation:

201N001466 AD55JGU0604140 VOLKSWAGEN GOLF S BLACK token 1 token 2 token 3 ( token * ).........................

echo !a:~0,4!1!a:~4!%%b %%c !a:~0,4! first 4 chars from token 1 1 the inserted "1" !a:~4! Chars 5 to end of token 1 (counting starts at zero) %%b token 2 %%c token * (Rest of the line)

Upvotes: 1

foxidrive
foxidrive

Reputation: 41234

Here is a robust method.

This uses a helper batch file called repl.bat - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place repl.bat in the same folder as the batch file or in a folder that is on the path.

@echo off
type "file.txt" | repl "(.*?N)(.*?) {1,}(.*)" "$11$2$3" >"newfile.txt"

Upvotes: 1

Zennsworld
Zennsworld

Reputation: 11

The way I would go at this is making 7 variables for the numbers that correspond to the space(use what ever you have for the other strings(with different names)) like:

set num1=201N
set num2=001466
set num3=001437
set num4=001298
set num5=AD55JGU0604140
set num6=AF14HFY0604140
set num7=AF51NSN0604140

And when you echo it (if you echo it) with the spaces you would do it like so (first line):

echo %num1%%num2%  %num5%  Golf S Black

And when you go to add a '1' to the lines you make one more variable and add the line (make a separate variable for each line)

set num8=%num1%1%num2%%num5% Golf S Black

And just do that for the other two lines.

Hope that helps, Thanks!

Upvotes: 0

Related Questions