-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitt
More file actions
105 lines (88 loc) · 2.86 KB
/
splitt
File metadata and controls
105 lines (88 loc) · 2.86 KB
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
#!/usr/bin/env lua
--[[
awk fail to extract columns from more than one file.
This little script provide a simple solution to extract
a column from each file. Field separator is space or any other
non alphanumeric character. It uses the lua %w format, but
a bit more complex: "[%d%a%.]+" line 76.
splitt file1 columnX .. fileN columnZ
Written when stranded overnight at Arlanda Airport from Umeå to Oslo
by Ole W. Saastad, 20 May 2016.
Last update 7. Nov 2024.
]]
-- Process arguments, how many files to process.
if #arg==0 then
print("usage: splitt file1 columnX .. fileN columnZ")
os.exit(1)
end
if not (#arg%2==0) then
print("Number of arguments must even")
print("usage: splitt file1 columnX file2 columnY file3 columnZ ....")
os.exit(1)
end
files=#arg/2
-- Some arrays to store each column's values
local f={} -- files, an array of file pointers.
local c={} -- column numbers, just a simple numeric array.
local i = 0
-- Read the arguments
for j = 1, files*2, 2 do
i = i + 1
f[i]=io.open(arg[j],"r") -- keep file pointers in an array.
if f[i]==nil then
print("usage: splitt file1 columnX .. fileN columnZ")
print("usage: failed to open file:",arg[j])
os.exit(1)
end
end
i = 0
for j = 2, files*2, 2 do
i = i + 1
c[i]=tonumber(arg[j])
if not c[i] then
print("usage: splitt file1 columnX .. fileN columnZ")
print("Expected number got :",arg[j])
os.exit(1)
end
end
-- some extra variables to do process the data
local col={}
local line={}
local pos = 0
local word = 0
local eol = false
local eof = false
for i = 1, files, 1 do
line[i] = f[i]:read()
end
while not eof do -- While still more lines to read from the files.
for i = 1, files, 1 do -- loop through all the files given
eol = false -- No end of line
word = 0
pos = 0
while not eol do -- parse the line in the i'th file.
local s, e = string.find(line[i], "[%d%a%.]+", pos) -- %w alphanumeric chars
if s then -- found a word?
pos = e + 1 -- next position is after this word
word = word + 1
if word == c[i] then
col[i]=string.sub(line[i], s, e) -- extract the word.
eol= true -- no need to read any more after we found the col.
end
else -- if s, false no more words.
print()
print("Column ",c[i]," not found in file",arg[i*2-1])
print("Giving up")
os.exit(1)
end -- if s
end -- while not eol
io.write(string.format("%s ",col[i])) -- print out the column value.
end -- for i = 1, files, 1
print()
for i = 1, files, 1 do
line[i] = f[i]:read() -- Read the next line from the files.
end
for i = 1, files, 1 do
if line[i]==nil then eof = true end -- End of file ?
end
end -- while not eof, if no more to read we're done.