Strip trailing spaces

March 20, 2011 at 06:06:48
Specs: Windows Vista
I have an example text file "file.txt" below and need to strip varying trailing spaces from last field using DOS batch command. Any help would be very appreciated.

"","00123450000000","LAMB KIDNEYS | 1KGM X 1 "
"","00543210000000","INSTORE CHICKEN & MUSHROOM PIE | 1KGM "
"","00546320000000","INSTORE HOT ROAST BEEF | 1PCE "

thanks in advance!

See More: Strip trailing spaces

Report •

March 20, 2011 at 06:13:16
Here's a Ruby for Windows one liner

C:\work>ruby -ne 'print $_.gsub(/\s+\"$/,"\"")' file
"","00123450000000","LAMB KIDNEYS | 1KGM X 1"
"","00543210000000","INSTORE CHICKEN & MUSHROOM PIE | 1KGM"
"","00546320000000","INSTORE HOT ROAST BEEF | 1PCE"

Report •

March 20, 2011 at 11:23:19
thank you for reply but
Is it possible to use a DOS batch script to remove extra spaces. The last field in the text file is 50 characters long in each record.
"","00123450000000","LAMB KIDNEYS | 1KGM X 1........................... "
I have a batch script running to generate the example file. One other small problem you might know. If I receive a file where someone has entered a comma in the text field is there a way to use a variable "%%n text field" in the batch to replace any comma with a space? This would help me greatly. Im learning fast....but only with your genius knowledge of Dos. Thanks for your help

Report •

March 20, 2011 at 15:25:08
Save the script as the name you like, then type e.g.

mybat Your_FileName

and you get the filtered file in

@echo off & setlocal EnableDelayedExpansion > ""
for /F "delims=" %%i in ('type "%~f1"') do (
  set fld=@
  for %%j in (%%~i) do set fld=!fld:,= ! %%j
  set fld=!fld:~2!
  set fld="!fld!"
  set fld=!fld:" =",!
)>> ""

By the way this is NT batch, more powerful than plain DOS that doesn't exist in XP/Vista. Ah... the issue of comma is solved too.

Report •

Related Solutions

Ask Question