-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_as_php_array.sh
90 lines (67 loc) · 1.64 KB
/
copy_as_php_array.sh
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
cat | perl -e '
# read first line to get the column names (header)
$firstLine = <>;
# bail if nothing could read
if(!defined($firstLine)) {
exit 0;
}
# store the column names
chomp($firstLine);
$firstLine =~ s/\"/\\\"/g; # escape "
@header = split(/\t/, $firstLine);
$h_cnt = $#header; # number of columns
# get the column definitions
open(META, $ENV{"SP_BUNDLE_INPUT_TABLE_METADATA"}) or die $!;
@meta = ();
while(<META>) {
chomp();
my @arr = split(/\t/);
push @meta, \@arr;
}
close(META);
print "[\n";
# read row data of each selected row
$rowData=<>;
while($rowData) {
print "\t[\n";
# remove line ending
chomp($rowData);
# escape "
$rowData=~s/\"/\\\"/g;
# split column data which are tab-delimited
@data = split(/\t/, $rowData);
for($i=0; $i<=$h_cnt; $i++) {
# re-escape \t and \n
$cellData = $data[$i];
$cellData =~ s/↵/\n/g;
$cellData =~ s/⇥/\t/g;
print "\t\t\"$header[$i]\" => ";
# check for data types
if($cellData eq "NULL") {
print "null";
}
elsif($meta[$i]->[1] eq "integer" || $meta[$i]->[1] eq "float") {
chomp($cellData);
$d = $cellData+0;
print "$d";
} else {
chomp($cellData);
print "\"$cellData\"";
}
# suppress last ,
if($i<$h_cnt) {
print ",";
}
print "\n";
}
print "\t]";
# get next row
$rowData=<>;
# suppress last ,
if($rowData) {
print ",";
}
print "\n";
}
print "]";
' | __CF_USER_TEXT_ENCODING=$UID:0x8000100:0x8000100 pbcopy