Difference between revisions of "Bash CSV Handling"

From TheBeard Science Project Wiki
Jump to: navigation, search
(Created page with "I'm sick of screwing this up, so I'm creating this definitive guide for my own reference. I will update this as I learn to handle individual issues. = Quoted Strings with Com...")
 
(Quoted Strings with Commas)
Line 2: Line 2:
  
 
= Quoted Strings with Commas =
 
= Quoted Strings with Commas =
 +
 +
When your fields are separated by commas, and one of the fields has a quoted string with a comma in it, it can screw up the parsing of the fields.
 +
 +
Here is a sample CSV file <code>test.csv</code>:
 +
<pre>
 +
id,firstname,lastname,email,phone,hiredate
 +
1122,"Colin","Quinn","c.quinn@example.com",1112223333,"02/12/2017"
 +
2233,"Richard","Vos, Richard","r.vos@example.com",2223334444,"06/27/2019"
 +
3344,"Patrice","O'Neal","p.o'neal@example.com",3334445555,"08/11/2018"
 +
4455,"Jim","Jefferies","j.jefferies@example.com",,"09/11/2001"
 +
</pre>

Revision as of 14:00, 27 June 2019

I'm sick of screwing this up, so I'm creating this definitive guide for my own reference. I will update this as I learn to handle individual issues.

Quoted Strings with Commas

When your fields are separated by commas, and one of the fields has a quoted string with a comma in it, it can screw up the parsing of the fields.

Here is a sample CSV file test.csv:

id,firstname,lastname,email,phone,hiredate
1122,"Colin","Quinn","c.quinn@example.com",1112223333,"02/12/2017"
2233,"Richard","Vos, Richard","r.vos@example.com",2223334444,"06/27/2019"
3344,"Patrice","O'Neal","p.o'neal@example.com",3334445555,"08/11/2018"
4455,"Jim","Jefferies","j.jefferies@example.com",,"09/11/2001"