stripping spaces from text file input

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
loftusm
Posts: 4
Joined: 13 Sep 2013 21:08

stripping spaces from text file input

#1 Post by loftusm » 13 Sep 2013 21:16

Hi
I am reading in a csv file and want to strip all trailing spaces from each field and then output it to a new file.
I need some help.
----------------------------------------------------

@echo off

setlocal ENABLEDELAYEDEXPANSION
set inputfile=part_5.csv

for /f "tokens=1,2,3,4,5 delims=," %%a in (%inputfile%) do (
set str1=%%a
set str2=%%b
set str3=%%c
set str4=%%d
set str5=%%e

I guest the strip goes here!!!!!

echo str1 = "!str1!"
echo str2 = "!str2!"
echo str3 = "!str3!"
echo str4 = "!str4!"
echo str5 = "!str5!"


pause

)

rem example of part_5.csv file input data
rem ------------------------------------
rem Manufacturer,Product Code,Product Name,Price,Master Category ID
rem Honda ,80100969681,FENDER REAR R ,453.06,27
rem Honda ,80101ADR250X,REAR GUARD ,140.25,27
rem Honda ,80101ADR450X,REAR MUDGUARD ,144.52,27
rem Honda,80101GA7000ZA,COVER BODY*NH-24* ,92.75,27
rem Honda,80101GA7000ZC,COVER BODY*R-23* ,101.15,27
rem Honda,80101GBFJ60ZA,SET ILLUST*TYPE1* ,71.67,27
rem Honda,80101GBFJ80ZA,SET ILLUST*R254R* ,68.29,27
rem Honda,80101GBFK20ZA,SET ILLUST*TYPE1* ,62.77,27
rem Honda,80101GCFA20ZA,80101GCF840ZA ,56.03,27
rem Honda,80101GCFA60ZA,SET ILLUST*TYPE1* ,40.06,27
rem Honda,80101GCF670ZA,SET ILLUST*TYPE2* ,46.75,27

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: stripping spaces from text file input

#2 Post by ShadowThief » 13 Sep 2013 21:39

Since you have spaces in there that you want to keep, you first have to change the space-comma to just a comma, then you can split on commas.

Code: Select all

@echo off
setlocal enabledelayedexpansion
set inputfile=part_5.csv
set tempfile=tempfile.txt

:: Change the space-commas into commas
for /f "tokens=* delims=" %%A in (%inputfile%) do (
   set string=%%A
   echo !string: ,=,!>>%tempfile%
)

:: Split on comma delimiter
for /f "tokens=1,2,3,4,5 delims=," %%A in (%tempfile%) do (
   echo Manufacturer: %%A
   echo Product Code: %%B
   echo Product Name: %%C
   echo Price: %%D
   echo Master Category ID: %%E
)

del %tempfile%

loftusm
Posts: 4
Joined: 13 Sep 2013 21:08

Re: stripping spaces from text file input

#3 Post by loftusm » 13 Sep 2013 22:09

Thanks ST
can we remove all the trailing spaces from the comma back.
Your script removes 1 space a treat but I would like to get rid of all of them.

thanks so much for your help.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: stripping spaces from text file input

#4 Post by ShadowThief » 13 Sep 2013 22:12

Removing every single space?

Change

Code: Select all

echo !string: ,=,!>>%tempfile%
to

Code: Select all

echo !string: =!>>%tempfile%
and you're done

EDIT: oops, left a comma in there by accident :-\

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: stripping spaces from text file input

#5 Post by foxidrive » 14 Sep 2013 01:57

This uses a helper batch file called repl.bat from - viewtopic.php?f=3&t=3855

Code: Select all

type "part_5.csv"|repl " *," "," m>"newfile.csv"

loftusm
Posts: 4
Joined: 13 Sep 2013 21:08

Re: stripping spaces from text file input

#6 Post by loftusm » 14 Sep 2013 02:23

Thanks foxidrive
I can't believe how simple that was.
Much appreciated.

Just for my own personal growth and development are you able to fix my attempt. I am trying to remove the trailing spaces from each field of the input line on the way through the loop and reconstruct the line at the end.

@echo off
setlocal enabledelayedexpansion
set inputfile=part_5.csv

del newfile.txt

for /f "tokens=1,2,3,4,5 delims=," %%a in (%inputfile%) do (

for /l %%a in (1,1,31) do if "!a:~-1!"==" " set a=!a:~0,-1!
for /l %%b in (1,1,31) do if "!b:~-1!"==" " set b=!b:~0,-1!
for /l %%c in (1,1,31) do if "!c:~-1!"==" " set c=!c:~0,-1!
for /l %%d in (1,1,31) do if "!d:~-1!"==" " set d=!d:~0,-1!
for /l %%e in (1,1,31) do if "!e:~-1!"==" " set e=!e:~0,-1!

echo %%a,%%b,%%c,%%d,%%e >>newfile.txt

pause
)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: stripping spaces from text file input

#7 Post by foxidrive » 14 Sep 2013 04:33

You are reusing the loop variables with the five for /L commands, which creates another issue,
and you'd have to use the for /L loop variables in the compare commands to calculate the characters, and then remove the trailing spaces only, and not remove all spaces.

The 'repl.bat' helper batch file is a lot like the widespread SED tool (Stream EDitor) which is designed to use regular expressions to manipulate and edit text, but it uses built-in Windows scripting capability.
Dave has provided us with a very useful tool there.

loftusm
Posts: 4
Joined: 13 Sep 2013 21:08

Re: stripping spaces from text file input

#8 Post by loftusm » 14 Sep 2013 05:15

He certainly has.
thanks for you help.

Post Reply