Removing trailing spaces from a csv file

How do I remove all trailing spaces from each field in a csv file? I'm using vbscript and perl for some other data processing, so a small script in one of these languages would be perfect. Let's say that we have file1 as the source, and file2 as the output file without trailing spaces. Thanks in advance for your help.
isaacr25Asked:
Who is Participating?
 
RobSampsonCommented:
Sure, this will do that.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
strCSVSource = "CSV1.csv"
strCSVDest = "CSV2.csv"
Set objSource = objFSO.OpenTextFile(strCSVSource, intForReading, False)
Set objDest = objFSO.CreateTextFile(strCSVDest, True)
While Not objSource.AtEndOfStream
	arrLine = Split(objSource.ReadLine, ",")
	strLine = Trim(arrLine(0))
	If UBound(arrLine) > 0 Then
		For intField = 1 To UBound(arrLine)
			strLine = strLine & "," & Trim(arrLine(intField))
		Next
	End If
	If Left(strLine, 1) = """" Then strLine = Right(strLine, Len(strLine) - 1)
	If Right(strLine, 1) = """" Then strLine = Left(strLine, Len(strLine) - 1)
	objDest.WriteLine strLine
Wend
objSource.Close
objDest.Close
MsgBox "Done. Please see " & strCSVDest

Open in new window

0
 
Adam314Commented:
perl -ne 'print join(",",split(/\s*,/, $_))' file1.csv > file2.csv

On windows:
perl -ne "print join(',',split(/\s*,/, $_))" file1.csv > file2.csv
0
 
isaacr25Author Commented:
Adam,
Will this require perl to be installed on the machine running the script?  If so, this will not work since I just found out that installing perl is not a possibility.

VBScript may be my best option if this is the case. Let me know. Thanks.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Adam314Commented:
As is, yes, perl must be installed.  You could compile a perl script to an exe, but VBScript may be best.  I don't use VBScript though, so you'll have to have someone else help.
0
 
RobSampsonCommented:
Hi, in your CSV file, do you have fields surrounded in quotes, or are they just commas?  For example:
"field1","field2","field3"
or
field1,field2,field3

Regards,

Rob.
0
 
isaacr25Author Commented:
When opened in Notepad, only the commas are present. There are no quotes.
0
 
RobSampsonCommented:
OK, try this.  This will also remove preceding spaces, as it uses the Trim function.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
strCSVSource = "CSV1.csv"
strCSVDest = "CSV2.csv"
Set objSource = objFSO.OpenTextFile(strCSVSource, intForReading, False)
Set objDest = objFSO.CreateTextFile(strCSVDest, True)
While Not objSource.AtEndOfStream
	arrLine = Split(objSource.ReadLine, ",")
	strLine = Trim(arrLine(0))
	If UBound(arrLine) > 0 Then
		For intField = 1 To UBound(arrLine)
			strLine = strLine & "," & Trim(arrLine(intField))
		Next
	End If
	objDest.WriteLine strLine
Wend
objSource.Close
objDest.Close
MsgBox "Done. Please see " & strCSVDest

Open in new window

0
 
isaacr25Author Commented:
Rob,
This seems to work great for removing the trailing space. However, a double-quote still exists at the beginning and end of each record. Can we remove those with the script? Thanks.
0
 
isaacr25Author Commented:
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.