问题
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
回答1:
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);
回答2:
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
来源:https://stackoverflow.com/questions/53608847/remove-values-from-rows-under-specific-columns-in-csv-file