-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresplit.awk
executable file
·80 lines (78 loc) · 1.77 KB
/
resplit.awk
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
#!/usr/bin/gawk -f
@include "join.awk"
BEGIN {
true = 1
false = 0
RS="\0"
FPAT="([\"'[:space:]\\\\])|([^\"'[:space:]\\\\]+)"
}
{
escaped = false
singleQuoted = false
doubleQuoted = false
currentIndex = 0
items[0] = ""
for(i = 1; i <= NF; i++) {
if ($i == "\\") {
if (escaped) {
items[currentIndex] = items[currentIndex] $i
escaped = false
}
else {
escaped = true
}
}
else if ($i == "'") {
if (escaped) {
if (doubleQuoted) {
items[currentIndex] = items[currentIndex] "\\"
}
items[currentIndex] = items[currentIndex] $i
escaped = false
}
else if (doubleQuoted) {
items[currentIndex] = items[currentIndex] $i
}
else {
singleQuoted = ! singleQuoted
}
}
else if ($i == "\"") {
if (escaped) {
if (singleQuoted) {
items[currentIndex] = items[currentIndex] "\\"
}
items[currentIndex] = items[currentIndex] $i
escaped = false
}
else if (singleQuoted) {
items[currentIndex] = items[currentIndex] $i
}
else {
doubleQuoted = ! doubleQuoted
}
} else if ($i ~ /[[:space:]]/) {
if (escaped) {
items[currentIndex] = items[currentIndex] $i
escaped = false
}
else if (singleQuoted || doubleQuoted) {
items[currentIndex] = items[currentIndex] $i
}
else {
currentIndex++
while (i < NF && $(i+1) ~ /[[:space:]]/) {
i++
}
}
}
else {
if (escaped) {
items[currentIndex] = items[currentIndex] "\\"
escaped = false
}
items[currentIndex] = items[currentIndex] $i
}
}
print join(items,0,length(items),"\0")
}