Handling piped io in Lua -
i have searched lot can't find answers anywhere. trying following:
cat somefile.txt | grep somepattern | ./script.lua
i haven't found single resource on handling piped io in lua, , can't figure out how it. there way, non hackish way tackle it? preferably buffered lower memory usage, i'll settle reading whole file @ once if thats alternative.
it disappointing have write temp file , load program.
thanks in advance.
the standard lirary has io.stdin
, io.stdout
can use input , output without havig resort temporary files. can use io.read
isntead of somefile:read
, read stdin default.
http://www.lua.org/pil/21.1.html
the buffering responsibility of operating system providing pipes. don't need worry when writing programs.
edit: apparently when mentioned buffering thinking reading part of file opposed loading whole file string. io.read
can take numeric parameter read number of bytes input, returning nil
if no characters read.
local size = 2^13 -- buffer size (8k) while true local block = io.read(size) if not block break end io.write(block) end
Comments
Post a Comment