如何高效读取大型文本文件中的行数

我有一个大约有750,000行的大型文本文件,每隔几秒钟就会不断地更新,我想能够实时监视行数。我可以做到这一点,但响应时间非常长。

function GetFileSize( filename )
  local fp = io.open( filename )
  if fp == nil then
    return nil
  end
  file = {}
  for line in fp:lines() do
    if (file[line] ~= line) then
      table.insert(file, line)
    end
  end
  d(table.size(file))
  local filesize = fp:seek( "end" )
  fp:close()
  return filesize
end

我想要两样东西,文件大小(字节)和行数。

然而,一遍遍地读取整个文件并将其填充到包含750,000行的表中,不断地从上向下读取文件,会导致相当多的处理时间。

有没有一种方法可以获取文件大小和行数,而不会严重影响我的系统。

我猜想我必须创建一个永久性的表格在函数外部,其中你可以读取文件并将行添加到表格中。但是,我不知道该如何阻止它每隔几秒钟就复制自身。

我应该放弃行数计数,只保留字节返回,因为这不会减慢我系统的运行速度吗?或者,是否有一种高效的方法可以同时获得两个值。

谢谢!

点赞
用户107090
用户107090

尝试使用gsub读取整个文件并计算行数。 你需要测试是否对你来说快速。

t = f:read("*a")
_,n = t:gsub("\n","")
2017-04-23 23:52:43
用户1944004
用户1944004

为了获取以字节为单位的文件大小,请使用[Lua文件系统](https://keplerproject.github.io/luafilesystem/)。您可能希望使用`io.lines`迭代器获取行数。为了更好地执行后者,在<>中描述了一个技巧。

local file = arg[0] -- just use the source file for demo

-- 获取文件大小
local lfs = assert(require"lfs")
local attr = lfs.attributes(file)
print(attr.size)

-- 获取行数
local count = 0
for line in io.lines(file) do
   count = count + 1
end
print(count)
2017-04-24 00:13:36
用户2328287
用户2328287

我可以建议这个解决方案。它不需要读取整个大文件。

local function char_count(str, ch)
  local n, p = 0
  while true do
    p = string.find(str, ch, p, true)
    if not p then break end
    n, p = n + 1, p + 1
  end
  return n
end

local function file_info(name, chunk_size)
  chunk_size = chunk_size or 4096
  local f, err, no = io.open(name, 'rb')
  if not f then return nil, err, no end
  local lines, size = 0, 0
  while true do
    local chunk = f:read(chunk_size)
    if not chunk then break end
    lines = lines + char_count(chunk, '\n')
    size = size + #chunk
  end
  f:close()
  return size, lines
end

但是如果您只需要监视一个文件并计算其中的行数,则可以使用任何文件监视器解决方案。我使用了一个基于LibUV

2017-04-24 06:55:01