Solved

How do I strip out a second CR/LF pair that randomly appears in a flat file

Posted on 2013-05-21
6
755 Views
Last Modified: 2016-02-10
Hi!

I'm using SSIS to import some flat files.  They are fixed length and use a CRLF as a row delimiter.  However, there are some cases (like every 5000 lines) where there is a second CRLF pair.  This messes up my import and rejects a number of records.  I'm trying to figure out a way to remove any occurrences of <CR><LF><CR><LF> to just be <CR><LF>   Any suggestions?
0
Comment
Question by:ITMikeK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39185149
ok, this is what I will do, I will try to use a script component and replace the double crlf charecter with a single one.

the below code might be of some use (note this is not tested, so test it yourself and let us know)

 Dim data As String
 data = file.ReadToEnd()
data = data.replace(ControlChars.CrLf+ControlChars.CrLf,ControlChars.CrLf)

 Dim writer As New System.IO.StreamWriter(Dts.Variables("@[User::str_SourcePath]").Value.ToString(), False)
 writer.Write(data)
 writer.Flush()
 writer.Close()

Open in new window

0
 

Author Comment

by:ITMikeK
ID: 39185188
Shouldn't this be possible using the REPLACE transformation function with a derived column?   I'm wrestling with the syntax, but would be something like this:

COLUMN ALIAS: ENDOFROW

REPLACE(ENDOFROW,"\n\n","\n")   ?
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39185198
No, because SSIS will determine your end of row by the CRLF character.
SO, by the time the your replace function actually gets hit then SSIS might have converted the data from your file into rows and it might be struggling to assign the values to columns, where it might fail ( as there is an empty line where there are two carriage returns).

So, you have to do that before loading the file using the below script task.
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:ITMikeK
ID: 39185286
i haven't written a SSIS script before, but I am a proficient C# developer.  How would I add the script?  When I drop the component on my canvas and double click to edit it, it launches a new instance of VS and another solution.  Not sure how to proceed.
0
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39185302
Cool, I am a novice in SSIS too, so cannot help you out much here....
If I am with same issue then I will a C# executable which will convert the crlf+crlf charecters into one crlf charecter from the C# console program and add this to the calling script of SSIS as well...
0
 

Author Closing Comment

by:ITMikeK
ID: 39188615
I was able to strip out the offending characters with Notepad++ in about 30 seconds.  When I have more time, I'll look at automating this into a script.  Thanks for the direction.
0

Featured Post

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

691 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