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,[email protected],123456789,1988-08-08
 2,Jane Doe,[email protected],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

3
  • 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. Commented Dec 4, 2018 at 8: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 stackoverflow.com/questions/3507498/… for one example. Commented Dec 4, 2018 at 8: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. Commented Dec 4, 2018 at 8:59

2 Answers 2

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,[email protected],123456789,1988-08-08";
 String[] csvList = csv.Split(',');
 csvList[2] = "";
 csvList[3] = "";
 csv = String.Join(",", csvList);
Sign up to request clarification or add additional context in comments.

1 Comment

This was exactly what i was looking for. Thanks!
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.