Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing lines from a CSV based on field comparisons

Status
Not open for further replies.

steve4king

IS-IT--Management
Feb 6, 2007
154
US
I need a way to modify a text file. I know there are a hundred text editors out there, but I don't know of any that can do what I need so I thought I might need to build one in C#.

The fields in the file are delimited by tabs.
I need to remove row 2 and 3, if row 5's field 7 = row 5's field 9. Then comparing, and potentially removing line 4 and 5, row 6 and 7 etc..

This would need to continue through the whole file, comparing field 7 to field 9, then removing it, and it's subsequent line. The first field's value for all even lines would be 'A', the value for odd lines would be 'I'.

I've got some experience coding c#, but I'm not really sure how to start with this one so any suggestions would be welcome.

This may not be the best forum for such a question, but this group has been so helpful in the past. So I'm gunna apologize ahead of time for that.

Thanks,
-Steve
 
You can do the following:

create a class to represent a row and its fields.
open the file using a StreamReader.
parse each line to create a strong typed row object.
store the rows in a list or hashtable. you can process the rows here or process only after when the list is completed.

hth [wink]
 
So I guess it wouldn't work to do an inline modification of the existing file? Better to read the whole thing in and output a new file?

Even so, I've got a lot of homework to figure out how to parse the data I want to keep.

Thanks,
-Steve
 
C# is an OO language so naturally advice provided here will slant toward an OO approach. if you want straight grep/regex I would use PERL.
Now, the problem may be complex, but it doesn't have to be complicated. break it down into parts (simplify the complexity). then wire the parts together.

1. read csv file into rows
2. from a row create a domain object
*now you have a collection of domain objects
3. create the logic to compare a single domain object against another.
*the result of this may be a new domain object
4. iterate over the domain objects and preform the logic from step 3.
*you may now have a new collection of domain object
5. write each resulting object to a string (csv formatted)
6. write these resulting strings to a file (csv extension).

depending on the complexity you can break step 4 into multiple objects as well. each object responsible for exactly 1 responsibility.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
To save you some of the brain ache, you could look at to read the CSV file.
It implements IDataReader, so you can just open the file and read each row at a time, perform your logic and write the required lines to a new file.

hth

Ben

----------------------------------------------
Ben O'Hara
David W. Fenton said:
We could be confused in exactly the same way, but confusion might be like Nulls, and not comparable.
 
Thanks guys, all very helpful. Thanks for the link Ben, being unfamiliar with IDataReader, that helps a lot.

-Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top