Solved

Removing trailing spaces from a csv file

Posted on 2008-09-30
9
960 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:isaacr25
  • 4
  • 3
  • 2
9 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 22606998
perl -ne 'print join(",",split(/\s*,/, $_))' file1.csv > file2.csv

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

Author Comment

by:isaacr25
ID: 22607028
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
 
LVL 39

Expert Comment

by:Adam314
ID: 22607108
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 22609906
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:isaacr25
ID: 22610298
When opened in Notepad, only the commas are present. There are no quotes.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22610371
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
 

Author Comment

by:isaacr25
ID: 22615166
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
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 22619294
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
 

Author Closing Comment

by:isaacr25
ID: 31501607
Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now