I have a huge csv file from an export delimited by pipe
8CDAC2EF-9261-4B9F-9D41-87B460C467B0|SAMPLE1|SAMPLE 2|SAMPLE 3|2020-08-03 00:00:00
8CDAC2EF-9261-4B9F-9D41-87B460C467B0|SAMPLE1|SAMPLE
2|SAMPLE 3|2020-08-03 00:00:00
8CDAC2EF-9261-4B9F-9D41-87B460C467B0|SAMPLE1|SAMPLE 2|
|2020-08-03 00:00:00
8CDAC2EF-9261-4B9F-9D41-87B460C467B0|SAMPLE1|SAMPLE 2||2020-08-03 00:00:00
8CDAC2EF-9261-4B9F-9D41-87B460C467B0|SAMPLE1||SAMPLE 3|2020-08-03 00:00:00
Ideally, the first line is the expected format, however, since its raw data, there seems some new line character in the fields SAMPLE 2, SAMPLE 3. I would like to replace the new line or breaking characters in these fields with empty character so it becomes the format of the first line.
PS: The SAMPLE 2 and SAMPLE 3 could be null as well.
I wanted to make this to the below format, ie, double quote enclosed as below.
"8CDAC2EF-9261-4B9F-9D41-87B460C467B0"|"SAMPLE1"|"SAMPLE 2"|"SAMPLE 3"|"2020-08-03 00:00:00"
I tried the below code, it works fine, but breaks if there are any new line characters each line.
sed -e 's/^\|$/"/g' -e 's/|/"|"/g' input.csv
Edit: The accepted solution works fine, but since I have some kind of raw data, I had do follow some additional steps since some of the values contain | and other special strings which shouldn't be there.
What I did was following.
- I was able to control the delimiter from the export, I used
€since we find that it's not used in the data in any of our samples. - Then I replaced
",',|, with a space character. finally, the delimiter of€was replaced with|. - Then used the accepted answer to filter out line breaks
- Finally piped them to make those are enclosed in quotes.