Remove values from rows under specific columns in csv file

天大地大妈咪最大 提交于 2021-01-29 08:30:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!