解析文本字符串

我已经使用命令将正在运行的Windows进程列表创建到文本文件中:

tasklist > c:\mytasklist.txt. 结果如下:

映像名称                     进程 ID 会话名            会话#     内存使用
========================= ======== ================ =========== ============
System Idle Process              0 Services                   0         24 K
System                           4 Services                   0     12.408 K
smss.exe                       320 Services                   0      1.236 K
csrss.exe                      424 Services                   0      4.720 K
wininit.exe                    516 Services                   0      4.684 K
csrss.exe                      524 Console                    1      7.888 K
winlogon.exe                   572 Console                    1      7.764 K
services.exe                   620 Services                   0      9.532 K

等等...

我的问题: 如何解析文本文件中的行,以便我可以使用Lua脚本获得以下格式的输出:

  1. 进程ID - 进程名称 - 内存使用
  2. 输出自动按最大内存使用排序
点赞
用户6834680
用户6834680
local tasklist = io.popen"tasklist /fo csv /nh"
local list = {}
for line in tasklist:lines() do
   local exe, pid, mem = line:match'^"(.-)","(%d+)",.-"([^"]+)"$'
   table.insert(list, {pid = tonumber(pid), exe = exe, mem = tonumber((mem:gsub("%D", "")))})
end
tasklist:close()
table.sort(list, function(a, b) return a.mem > b.mem end)
for j = 1, math.min(10, #list) do
   print(list[j].pid, list[j].mem, list[j].exe)
end

输出:

1036  549416  svchost.exe
4972  439524  firefox.exe
6540  214476  plugin-container.exe
7144  169268  OUTLOOK.EXE
532   75320   svchost.exe
1948  71644   avp.exe
3752  62704   svchost.exe
5268  61100   explorer.exe
596   56732   csrss.exe
5048  50248   CcmExec.exe
2019-08-01 08:08:45