-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv2.awk
More file actions
35 lines (34 loc) · 1.02 KB
/
csv2.awk
File metadata and controls
35 lines (34 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# csv2.awk: convert csv data to tab-separated
BEGIN {
rc = getline
while (rc > 0) {
if (eat(" +")) { # leading blanks
/* just strip, no output */
} else if (eat(",")) { # a comma
printf("\t")
} else if (eat("\"[^\"]*\"")) { # a quoted string
printf("%s", substr(eaten, 2, length(eaten)-2))
# possibly with an embedded "", representing a single "
while (eat("\"[^\"]*\""))
printf("\"%s", substr(eaten, 2, length(eaten)-2))
} else if (eat("[^,]+")) { # a non-quoted string
sub(/ +$/, "", eaten) # strip any trailing blanks
printf("%s", eaten)
} else if ($0 == "") { # end of line
printf("\n")
rc = getline
}
}
}
# eat: try to remove match of regular expression re at start of $0
# If a match, save the matched string in global variable "eaten" and return 1
# otherwise return 0
function eat(re) {
if (match($0, "^" re)) {
eaten = substr($0, 1, RLENGTH)
$0 = substr($0, RLENGTH+1)
return 1
} else {
return 0
}
}