csv - How to output more than one column -
i want process data file this:
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 and output some columns.
so far managed:
local infile = arg[1] local outfile = arg[2] local column = tonumber(arg[3]) local data = {} local row = 0 local ofile line in io.lines(infile) row = row + 1 data[row] = {} local = 0 value in string.gmatch(line, "%s+") = + 1 data[row][i] = value end end ofile = assert(io.open(outfile, "w")) = 1,row ofile:write(data[i][column] .. "\n") end ofile:close() this works fine 1 column: lua column.lua test.dat new.dat 2
190 165 160 151 141 142 what have lua column.lua test.dat new.dat 1,2,4 have columns 1, 2 , 4 in new file. possible?
you can use following function extract list of columns:
function cols(t, colnums, prefix) -- number of first column , rest of numbers -- splits 1,2,3 1 , 2,3 local col, rest = colnums:match("^(%d+)[,%s]*(.*)") -- if nothing provided return current `prefix` value (may `nil`) if not col return prefix end -- convert string column number number -- needed because t[1] , t['1'] references different values local val = t[tonumber(col)] -- call same function recursively, using rest of columns -- concatenates prefix (if any) current value return cols(t, rest, prefix , prefix.."\t"..val or val) end now instead of ofile:write(data[i][column] .. "\n"), can use ofile:write(cols(data[i], arg[3]) .. "\n"). since parsing on every row, may inefficient large number of rows, if that's case you, you'll need parse once @ beginning of script.
Comments
Post a Comment