Link to home
Start Free TrialLog in
Avatar of rhservan
rhservanFlag for United States of America

asked on

A little spacy

Problem: I have a comma delimited file and the size is 4MB (this equals about 600K of data and the rest are spaces).  It is loaded with spaces before the comma's and after the comma's.  This file will be used to import into SQL and I need it to match other current criteria.  The data in the file is correct but all the spaces prevent me from performing joins.

Objective:  I need to remove all the spaces from this file and leave intact all row and field termination.  I want to be able to perform this with an unattended DOS command line script.
Avatar of moorhouselondon
moorhouselondon
Flag of United Kingdom of Great Britain and Northern Ireland image

Do a global replace of space comma with comma, repeated until no matches are found.
Ditto:- comma space with comma.

One thing this won't cope with is commas embedded in the text (which will have inverted commas around the field to make sure that the comma is treated as data, rather than a field delimiter.
Hi rhservan,

You'd be better off doing this inside th database. Much quicker and more reliable.

If not, there are many freeware search/replace tools. Why did you choose to do this with a DOS script?

Paul
Avatar of callrs
callrs

What do you mean by "leave intact all row and field termination"? What are the terminators? Are they spaces or are they commas or must they be comma + a space? If you need absolutely ALL spaces out, I can write you up something to do that.
Avatar of rhservan

ASKER

Paul,

I initially desired to perform in a DOS script because I already have other perparation and processes being performed from a dos sript such as: sqlcmd statements , mail to: notifications, bcp, directory builds etc..  I am using 2005 express version and much work has already been performed from dos scripts which run from schedules things run very fast in my script.

Please elaborate for me exactly how to perform this function in sql.  Include all statements or scripting I would require.

RS
callrs,
 
I want all spaces out.  So, all spaces can be removed. No other concerns.

Show me what you have be very specific.
I can make a simple C program, say "nospaces.exe" that you would use like this
nospaces inputfile.txt outputfile.txt
outputfile.txt would have all spaces removed
I'll make it & post it on my website for download if you promise me the points. Can have it ready by tomorrow.

A free batch find and replace utility is available at
http://www.dogsbyte.com/multisubdownload.html
D
Also see a previous post on EE which appears to give a pure batch file approach
https://www.experts-exchange.com/questions/20828466/Find-and-replace-text-in-Files.html
pertinent part from DrWarezz follows
D

@echo off

REM ------------ Change the Directory ------------
set DIR=c:\Directory
REM ----------------------------------------------

FOR /D /R %%a in (%DIR%\*) DO call :GETFILE %%a
goto :END


:GETFILE

cd %1
FOR %%f in (*.*) DO set FILE=%%f

FOR /f "tokens=1 delims=" %%i in (%FILE%) DO (

set TEXT=%%i
echo %TEXT:replace=replaced% >>newFile.txt
del %FILE%
ren newFile.txt %FILE%
)

:END
exit /b
;---------------------------------------------------------------------------------------------------------------

callrs, I will have to test it first to verify functionality.
callrs, let me know when you have it ready.

K, I'll get to work on it tonight. You can test it all you want before awarding points. Thanks.
callrs,

Please be sure to post the source to your solution here. We're not supposed to take things offline like that.

I'd suggest something trivial like:

char ch;
while ( (ch = fgetc (stdin)) != EOF )
{
 if ( ch != ' ' ) fputc(ch,stdout);
}
 
Paul <Page Editor>
Yes Paul, thanks. I had intended to post the code anyway, but will also supply rhservan with a compiled copy . Anyway I finished after almost 4 hours work lol. It's more than was asked for, with all error-checking and an extra option. Will post later, I need a break.
ASKER CERTIFIED SOLUTION
Avatar of callrs
callrs

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
callrs, would there any limitation to lines, rows or characters when the program is executed.

It reads the WHOLE file. Takes out ALL of the spaces (or other character-code you specify). No limitation at all.
Other than any possible limitations built into the operating system as regard to file size (I've read about 4Gig file-size limits etc.), and the space available on your disk to store the output file.
callrs, Thanks!!!
and I do compost.