Skip to content

Commit 61aee02

Browse files
authored
Handled bug where field is blank with ""
1 parent d6b6275 commit 61aee02

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

csv.awk

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Script: csv.awk
44
# Author: Ben Altman ([email protected])
5-
# Description: Convert csv files from Excel in to a format using a different separators.
5+
# Description: Convert csv files from Excel in to a format using a different separator.
66

77
# Usage: csv.awk [options] csv_file
88
# fs=csv_file_field_separator (default is comma)
@@ -40,6 +40,9 @@ BEGIN {
4040
# 1. A field with no DQs is a simple field and we can move straight to the next field.
4141
# 2. A field starting with a DQ can contain DQs, FS or multiple lines which we need to join.
4242

43+
# Exceptional even double quoted field where then entire field is "" is equal to blank without the double quotes
44+
if ($i == "\"\"") $i=""
45+
4346
# If the field is not a simple field with no DQs at all
4447
if (substr($i,1,1) == "\"") {
4548
# while the field ENDS in even or no DQs it is joined to the next field, until odd quotes are joined ending the field...

csv.bash

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Script: csv.bash
44
# Author: Ben Altman (csv.feedback.benalt at xoxy.net)
5-
# Description: Convert csv files from Excel in to a format using a different separators.
5+
# Description: Convert csv files from Excel in to a format using a different separator.
66
#
77
# Usage: csv.bash [options] csv_file
88
# -i csv_file_field_separator (default is comma)
@@ -56,6 +56,9 @@ BEGIN {
5656
i=1; j=2
5757
n = nf = split($0, field)
5858
59+
# Exceptional even double quoted field where then entire field is "" is equal to blank without the double quotes
60+
if ($i == "\"\"") $i=""
61+
5962
while (j <= n+1) {
6063
# 1. A field with no DQs is a simple field and we can move straight to the next field.
6164
# 2. A field starting with a DQ can contain DQs, FS or multiple lines which we need to join.

csv.ksh

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Script: csv.ksh
44
# Author: Ben Altman (csv.feedback.benalt at xoxy.net)
5-
# Description: Convert csv files from Excel in to a format using a different separators.
5+
# Description: Convert csv files from Excel in to a format using a different separator.
66
#
77
# Usage: csv.bash [options] csv_file
88
# -i csv_file_field_separator (default is comma)
@@ -44,7 +44,7 @@ BEGIN {
4444
if (! nl) nl = "\\n"
4545
if (! fs) FS = ","; else FS = fs
4646
if (! ofs) OFS = "|"; else OFS=ofs
47-
above line backs up FS for when rejoining fields containing FS in the case when it`s a pipe. Don`t join with [|] per the below
47+
# above line backs up FS for when rejoining fields containing FS in the case when it`s a pipe. Don`t join with [|] per the below
4848
gsub("[|]","[|]",FS) # Input separator containing a pipe replace "|" with "[|]"
4949
}
5050
@@ -60,6 +60,9 @@ BEGIN {
6060
# 1. A field with no DQs is a simple field and we can move straight to the next field.
6161
# 2. A field starting with a DQ can contain DQs, FS or multiple lines which we need to join.
6262
63+
# Exceptional even double quoted field where then entire field is "" is equal to blank without the double quotes
64+
if ($i == "\"\"") $i=""
65+
6366
# If the field is not a simple field with no DQs at all
6467
if (substr($i,1,1) == "\"") {
6568
# while the field ENDS in even or no DQs it is joined to the next field, until odd quotes are joined ending the field...

0 commit comments

Comments
 (0)