0

I'm currently trying to remove specific values from rows under specific columns in a CSV-file.

Whats the best way of doing this?

Is it to use a XSLT map file in the code or doing this only by code? (Using c#)

What I want to do is this:

BEFORE MANIPULATION:

 id, name, email, phoneNumber, dob 
 1,John Doe,JohnDoe@mail.com,123456789,1988-08-08
 2,Jane Doe,JaneDoe@mail.com,987654321,1987-07-07

AFTER MANIPULATION:

 id, name, email, phoneNumber, dob 
 1,John Doe,,,1988-08-08 
 2,Jane Doe,,,1987-07-07

As you can see "email" and "phoneNumber" is gone

Tim C
  • 70,053
  • 14
  • 74
  • 93
H4p7ic
  • 1,669
  • 2
  • 32
  • 61
  • 1
    Please edit your question and show us the code you are currently working with and describe any issues you are having with that code. – Linda Lawton - DaImTo Dec 04 '18 at 08:52
  • 1
    XSLT is really designed for converting XML files. Whilst not impossible to handle CSV files, you are really better off doing it by code. See https://stackoverflow.com/questions/3507498/reading-csv-files-using-c-sharp for one example. – Tim C Dec 04 '18 at 08:55
  • It's not difficult, so I would do with code. There might be some tool that would let you do it with less code, but the time taken to learn it would remove any benefit (and a future maintainer would probably rewrite it from scratch). Just read the file into a set of variables, one line at a time. Clear any variables you want, and write them to a new file. – Robin Bennett Dec 04 '18 at 08:59

2 Answers2

0

Best way very much depends on personal preferences. For me the best way would be to use sed. Assuming your data is in data.csv file:

cat data.csv | sed '2,$ s/\([^,]*\),\(.[^,]*\),\(.[^,]*\),\([^,]*\),\([^,]*\)/\1,\2,,,\5/' > output.csv
helcim
  • 789
  • 13
  • 27
0

You can use C# without any libs to separate and joint csv strings. it's easier than use XLST. As sample:

 String csv = "1,John Doe,JohnDoe@mail.com,123456789,1988-08-08";
 String[] csvList = csv.Split(',');
 csvList[2] = "";
 csvList[3] = "";
 csv = String.Join(",", csvList);
sergey p
  • 28
  • 5