-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextractrange.lua
executable file
·54 lines (43 loc) · 1.22 KB
/
extractrange.lua
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
#!/usr/bin/env luajit
local io = require("io")
local os = require("os")
local fileName = arg[1]
local startPattern = arg[2]
local stopPattern = arg[3]
if (fileName == nil or startPattern == nil or stopPattern == nil) then
print("Usage: "..arg[0].." <fileName> <startPattern> <stopPattern>")
print("")
print("Extracts all lines from the first one matching <startPattern>")
print("up to and including the first one matching <stopPattern>.")
print("")
print("Exit codes:")
print(" 0: at least one line was extracted")
print(" 1: no lines were extracted")
print(" 2: failed opening file")
os.exit(1)
end
local function main()
local f, errMsg = io.open(fileName)
if (f == nil) then
print("Error opening file: "..errMsg)
os.exit(1)
end
local extracting = false
while (true) do
local line = f:read("*l")
if (line == nil) then
break
end
if (not extracting and line:match(startPattern)) then
extracting = true
end
if (extracting) then
print(line)
if (line:match(stopPattern)) then
return 0
end
end
end
return 1
end
return main()