-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsqlGen.awk
More file actions
52 lines (50 loc) · 1001 Bytes
/
sqlGen.awk
File metadata and controls
52 lines (50 loc) · 1001 Bytes
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
#!/bin/awk -f
BEGIN{\
FS="|"; \
head="insert into ";\
fieldName = ""; \
fieldValue = "";\
} \
#exe block start
{ \
#start if
if(2 == NR){ \
for(i=1;i<NF;i++){\
if($i==""){ \
continue;
}
if(i==NF-1){
fieldName = fieldName trim($i); \
}else{
fieldName = fieldName trim($i) ","; \
}
#end for
} \
#print fieldName; \
#get the file name
getTableName(FILENAME);
#end if
}else if(NR > 2){
fieldValue = "";
for(i=1;i<NF;i++){\
if($i==""){ \
continue;
}
if(i==NF-1){
fieldValue = fieldValue "'"trim($i)"'";
}else{
fieldValue = fieldValue "'"trim($i)"'" ",";
}
#end for
}
#print the sql statement
if(fieldValue != ""){
print head getTableName(FILENAME) "("fieldName")""values" "("fieldValue")"";";
}
}
#end exe block
}
function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s }
function trim(s) { return rtrim(ltrim(s)); }
function getTableName(name){if(match(name,".")!=0){split(name,tableName,".");return tableName[1];}}