如何输出多列
How to output more than one column
我想处理这样的数据文件:
2015-02-23 190 170 131 14 8 9 130 85 102.0 12 68
2015-02-24 165 128 97 14 7 6 110 75 101.7 12 64
2015-02-25 160 123 129 11 5 7 130 85 101.3 12 68
2015-02-26 151 115 128 11 nan 7 120 80 100.9 12 64
2015-02-27 141 119 130 11 4 nan 130 85 101.6 12 68
2015-02-28 142 137 143 nan nan nan 120 80 101.2 12 64
并输出一些列。
到目前为止我做到了:
local infile = arg[1]
local outfile = arg[2]
local column = tonumber(arg[3])
local data = {}
local row = 0
local ofile
for line in io.lines(infile)
do
row = row + 1
data[row] = {}
local i = 0
for value in string.gmatch(line, "%S+") do
i = i + 1
data[row][i] = value
end
end
ofile = assert(io.open(outfile, "w"))
for i = 1,row do
ofile:write(data[i][column] .. "\n")
end
ofile:close()
这适用于一列:lua column.lua test.dat new.dat 2
190
165
160
151
141
142
我想要的是 lua column.lua test.dat new.dat 1,2,4
在新文件中包含第 1、2 和 4 列。这可能吗?
您可以使用以下函数提取列列表:
function cols(t, colnums, prefix)
-- get the number of the first column and the rest of the numbers
-- it splits 1,2,3 into 1 and 2,3
local col, rest = colnums:match("^(%d+)[,%s]*(.*)")
-- if nothing is provided return current `prefix` value (may be `nil`)
if not col then return prefix end
-- convert the string with the column number into number
-- this is needed because t[1] and t['1'] references different values
local val = t[tonumber(col)]
-- call the same function recursively, but using the rest of the columns
-- this also concatenates the prefix (if any) with the current value
return cols(t, rest, prefix and prefix.."\t"..val or val)
end
现在您可以使用 ofile:write(cols(data[i], arg[3]) .. "\n")
而不是 ofile:write(data[i][column] .. "\n")
。由于它会在每一行上进行解析,因此对于大量行来说效率可能很低,因此如果您遇到这种情况,您可能需要在脚本开头解析一次。
我想处理这样的数据文件:
2015-02-23 190 170 131 14 8 9 130 85 102.0 12 68
2015-02-24 165 128 97 14 7 6 110 75 101.7 12 64
2015-02-25 160 123 129 11 5 7 130 85 101.3 12 68
2015-02-26 151 115 128 11 nan 7 120 80 100.9 12 64
2015-02-27 141 119 130 11 4 nan 130 85 101.6 12 68
2015-02-28 142 137 143 nan nan nan 120 80 101.2 12 64
并输出一些列。
到目前为止我做到了:
local infile = arg[1]
local outfile = arg[2]
local column = tonumber(arg[3])
local data = {}
local row = 0
local ofile
for line in io.lines(infile)
do
row = row + 1
data[row] = {}
local i = 0
for value in string.gmatch(line, "%S+") do
i = i + 1
data[row][i] = value
end
end
ofile = assert(io.open(outfile, "w"))
for i = 1,row do
ofile:write(data[i][column] .. "\n")
end
ofile:close()
这适用于一列:lua column.lua test.dat new.dat 2
190
165
160
151
141
142
我想要的是 lua column.lua test.dat new.dat 1,2,4
在新文件中包含第 1、2 和 4 列。这可能吗?
您可以使用以下函数提取列列表:
function cols(t, colnums, prefix)
-- get the number of the first column and the rest of the numbers
-- it splits 1,2,3 into 1 and 2,3
local col, rest = colnums:match("^(%d+)[,%s]*(.*)")
-- if nothing is provided return current `prefix` value (may be `nil`)
if not col then return prefix end
-- convert the string with the column number into number
-- this is needed because t[1] and t['1'] references different values
local val = t[tonumber(col)]
-- call the same function recursively, but using the rest of the columns
-- this also concatenates the prefix (if any) with the current value
return cols(t, rest, prefix and prefix.."\t"..val or val)
end
现在您可以使用 ofile:write(cols(data[i], arg[3]) .. "\n")
而不是 ofile:write(data[i][column] .. "\n")
。由于它会在每一行上进行解析,因此对于大量行来说效率可能很低,因此如果您遇到这种情况,您可能需要在脚本开头解析一次。