I have a csv file that has line breaks inside fields like this :
"abc" , "adef
efw", "abc"
"abc","awe","abc"
Which should be :
"abc", "adefefw","abc"
"abc","awe","abc"
Does anyone know some tools or script to fix this ? I prefer using Vim.
I have a csv file that has line breaks inside fields like this :
"abc" , "adef
efw", "abc"
"abc","awe","abc"
Which should be :
"abc", "adefefw","abc"
"abc","awe","abc"
Does anyone know some tools or script to fix this ? I prefer using Vim.
If all your fields are quoted with double quotes as in your example, you can simply remove all newline characters that follow a non-doublequote:
%s/\([^"]\)\n/\1/
For those looking for a non-interactive solution, sed
is hard to work with when it comes to replacing newlines. Using perl, however, you can use the same expression as above:
perl -p -e 's/([^"])\n/\1/'
You have a carriage return or line break formatted in. Show all character in your text editor (I use notepad++) and you should see:
\n or \r
\n is a line feed and \r is a carriage return, different systems use either one or both.
Simply move them where they should be. Differences of \n and \r
You may be able to use preg_replace() function with PHP but you would need to parse the CSV.
Another option might be to user the str_getcsv() function in PHP.
Example from page..
<?php
$csv = array_map('str_getcsv', file('data.csv'));
?>
Once you parse it into an array you can then output it and reformat it how you would like.