-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniq.vbs
executable file
·193 lines (188 loc) · 4.84 KB
/
uniq.vbs
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'
' Run as cscript //NoLogo uniq.vbs [-d|-c] [input ...]
'
'--------------------------------------------------
' 共通処理
'
Option Explicit
Function include(filename)
ExecuteGlobal CreateObject("Scripting.FileSystemObject").OpenTextFile(filename).ReadAll()
End Function
include("mytoolkit.vbs")
'--------------------------------------------------
' 大局変数
'
Dim objEndSw : Set objEndSw = new MySwitch
Dim objMisc : Set objMisc = new MyMisc
' User coding start
Dim objOpt : Set objOpt = new MyOption
Dim objStdio : Set objStdio = new MyStdio
Dim objFsoIn : Set objFsoIn = new MyFso
Dim strRec
Dim objSkipSw : Set objSkipSw = new MySwitch
Dim strArrayFilesEtc
Dim intArrayFilesEtcIndex : intArrayFilesEtcIndex = 0
Dim strOldRec : strOldRec = ""
Dim strNewRec : strNewRec = ""
Dim intCount : intCount = 0
Dim objFirstRecSw : Set objFirstRecSw = new MySwitch : objFirstRecSw.turnOn
Dim objFirstDupSw : Set objFirstDupSw = new MySwitch : objFirstDupSw.turnOn
Dim objUniqSw : Set objUniqSw = new MySwitch : objUniqSw.turnOn
Dim objDupSw : Set objDupSw = new MySwitch
Dim objCountSw : Set objCountSw = new MySwitch
' User coding end
'--------------------------------------------------
' 処理開始
'
sub_open ' オープン処理
sub_initialize ' 開始処理
While objEndSw.isOff
sub_main ' 主処理
Wend
sub_terminate ' 終了処理
sub_close ' クローズ処理
objMisc.exitProg(0)
'--------------------------------------------------
' オープン処理
'
Sub sub_open
' User coding start
objOpt.initialize("-h=n,--help=n,-d=n,-c=n")
If objOpt.isSpecified("-h") or objOpt.isSpecified("--help") Then
objStdio.writeLine "Usage : cscript //NoLogo uniq.vbs [-d|-c] [input ...]"
objStdio.writeLine "Filter adjacent matching lines from input (or standard input), writing to standard output."
objStdio.writeLine "With no options, matching lines are merged to the first occurrence."
objStdio.writeLine ""
objStdio.writeLine " -d only print duplicate lines."
objStdio.writeLine " -c prefix lines by the number of occurrences."
objEndSw.turnOn
End If
strArrayFilesEtc = objOpt.getArrayNonOptions
If objEndSw.isOff Then
If objOpt.isSpecified("-d") Then
objDupSw.turnOn
objUniqSw.turnOff
End If
If objOpt.isSpecified("-c") Then
objCountSw.turnOn
objUniqSw.turnOff
End If
openFile
End If
' User coding end
End Sub
'--------------------------------------------------
' 開始処理
'
Sub sub_initialize
' User coding start
If objEndSw.isOff Then
strRec = readRec
End If
' User coding end
End Sub
'--------------------------------------------------
' 主処理
'
Sub sub_main
' User coding start
If objSkipSw.isOff Then
strNewRec = strRec
If objUniqSw.isOn Then
If objFirstRecSw.isOn Then
objStdio.writeLine strNewRec
objFirstRecSw.turnOff
Else
If strNewRec <> strOldRec Then
objStdio.writeLine strNewRec
End If
End If
strOldRec = strNewRec
Elseif objDupSw.isOn Then
If objFirstRecSw.isOn Then
objFirstRecSw.turnOff
Else
If strNewRec = strOldRec Then
If objFirstDupSw.isOn Then
objStdio.writeLine strNewRec
objFirstDupSw.turnOff
End If
Else
objFirstDupSw.turnOn
End If
End If
strOldRec = strNewRec
Elseif objCountSw.isOn Then
If objFirstRecSw.isOn Then
intCount = intCount + 1
objFirstRecSw.turnOff
Else
If strNewRec = strOldRec Then
intCount = intCount + 1
Else
objStdio.writeLine intCount & " " & strOldRec
intCount = 1
End If
End If
strOldRec = strNewRec
End If
End If
strRec = readRec
' User coding end
End Sub
'--------------------------------------------------
' 終了処理
'
Sub sub_terminate
' User coding start
If objCountSw.isOn Then
objStdio.writeLine intCount & " " & strOldRec
End If
' User coding end
End Sub
'--------------------------------------------------
' クローズ処理
'
Sub sub_close
' User coding start
closeFile
' User coding end
End Sub
'--------------------------------------------------
' その他の処理
'
' User coding start
Function openFile
If UBound(strArrayFilesEtc) >= 0 Then
objEndSw.turnOff
objFsoIn.openInput(strArrayFilesEtc(intArrayFilesEtcIndex))
intArrayFilesEtcIndex = intArrayFilesEtcIndex + 1
End If
End Function
Function closeFile
If UBound(strArrayFilesEtc) >= 0 Then
objFsoIn.close
End If
End Function
Function readRec
If UBound(strArrayFilesEtc) >= 0 Then
readRec = objFsoIn.readLine
If objFsoIn.isReadFailure Then
If intArrayFilesEtcIndex <= UBound(strArrayFilesEtc) Then
closeFile
openFile
objSkipSw.turnOn
Else
objEndSw.turnOn
End If
Else
objSkipSw.turnOff
End If
Else
readRec = objStdio.readLine
If objStdio.isReadFailure Then
objEndSw.turnOn
End If
End If
End Function
' User coding end