-1

I need to remove rows from CSV file using C# in SSIS

Here is my file

XXXX,,,,,,,
XXXX111,,,,,,,
XXXX222,,,,,,,
A,b,c,d,e,f
g,h,i,j,k,l
1,2,3,4,5,6
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,

Here is how my output should look like

A,b,c,d,e,f
g,h,i,j,k,l
1,2,3,4,5,6

Basically i need to remove

XXXX,,,,,,,
XXXX111,,,,,,,
XXXX222,,,,,,, 
,,,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,

Thanks in advance

user1570210
  • 1,169
  • 12
  • 26
  • 37
  • Why do you want to remove those rows? Think about the rules, and show us how far you've got implementing them, and it'll be possible to help you out. – Rowland Shaw Jul 25 '13 at 18:14
  • you need a criteria for removal. Any row with and empty columns? any row with columns more than one character? etc. – Jonesopolis Jul 25 '13 at 18:16
  • Can I do based on number of times commas occur simultaneously ? Like remove rows where have more than 5 commas simultaneously like ,,,,, ? – user1570210 Jul 25 '13 at 18:18
  • Here's the code you want: `foreach (var line in ReadAllLines()); { if (IsGoodLine(line)) WriteLine(line); }`. Implementing `ReadAllLines()`, `IsGoodLine()` and `WriteLine()` are an exercise for the reader. – Bobson Jul 25 '13 at 18:20

1 Answers1

2

Here's a simple solution based on your 5 commas criteria

List<String> lines = new List<string>();
string line;
System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.txt");

while ((line = file.ReadLine()) != null)
{
    lines.Add(line);
}

lines.RemoveAll(l => l.Contains(",,,,,"));

Then you can write it back out or whatever you'd like

Writing out :

using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(outputPath))
{
      outfile.Write(String.Join(System.Environment.NewLine, lines.ToArray()));
}   
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112