简体   繁体   English

如何创建SSIS脚本任务以从平面文件中删除CR LF

[英]How to create SSIS script task to remove the CR LF from a flat file

We have an SSIS job that takes data from a database and outputs it to a csv file and saves it to a file share. 我们有一个SSIS作业,该作业从数据库获取数据并将其输出到csv文件并将其保存到文件共享。 The last line in the csv we produce is a blank row and the consumers of this file want it removed. 我们生成的csv的最后一行是空白行,此文件的使用者希望将其删除。 I need to figure out how, after we write the file to the directory, to modify the file and remove that last blank row. 在将文件写入目录之后,我需要弄清楚如何修改文件并删除最后一个空白行。 I believe you can do this in a script task but everything I have tried has failed, both VB and C#. 我相信您可以在脚本任务中执行此操作,但是我尝试过的所有操作都失败了,包括VB和C#。 Has anyone figured out how to do something like this? 有没有人想出如何做这样的事情?

I hope this works as I am shooting from the hip: 我希望这能在我从臀部射击时起作用:

This should remove the last carriage return and line feed. 这应该删除最后的回车和换行。

C# C#

string s = System.IO.File.ReadAllText([filePath]); 
s=s.Substring(1,s.Length-2); //CRLF
System.IO.File.WriteAllText([filePath],s);

You can use a Conditional Split Transformation in between source and destination on a Data Flow pipeline to remove empty rows. 您可以在数据流管道上的源和目标之间使用条件拆分转换来删除空行。

在此处输入图片说明

In the Split Transformation Editor specify a boolean expression with a Condition like 在分割转换编辑器中,指定条件为

LEN([Col 1]) > 0 && LEN([Col 2]) > 0 ...

在此处输入图片说明

Bonus: If there is also bad data in the output file you may want to filter on a different length (as shown in the screenshot): Ref . 奖励:如果输出文件中还有不良数据,您可能需要使用其他长度进行过滤(如屏幕截图所示): Ref


If this still does not work I suggest to try it with a C# script task along that lines: 如果仍然无法解决问题,我建议尝试使用C#脚本任务来尝试:

try
{
    string input = System.IO.File.ReadAllText("path");
    string output = System.Text.RegularExpressions.Regex.Replace(input, @"^\s+$[\r\n]*", "", System.Text.RegularExpressions.RegexOptions.Multiline);
    System.IO.File.WriteAllText("path", output);
}
catch (Exception e)
{
    //Log..
    Dts.TaskResult = (int)ScriptResults.Failure;
}

If you also want to remove the very last line-break use this pattern with the code above instead: @"^\\s+$[\\r\\n]*|[\\r\\n]$" 如果您还想删除最后一个换行符,请使用上面的代码使用此模式: @"^\\s+$[\\r\\n]*|[\\r\\n]$"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM