我现在的密码是..。
print_line
processes = @client.sys.process.get_processes
blacklist = ["NT AUTHORITY\\SYSTEM", "NT AUTHORITY\\LOCAL SERVICE", "NT AUTHORITY\\NETWORK SERVICE"]
filtered = processes.map!{|i| i.slice!("arch", "session", "path")}.reject {|h| blacklist.include? h['user']}
filtered.each do |r|
puts r.map { |p| p}.join(" ")
end这是输出..。
pid 0 ppid 0 name [System Process] user
pid 456 ppid 320 name explorer.exe user CLINE\Administrator
pid 544 ppid 204 name TPAutoConnect.exe user CLINE\Administrator
pid 1096 ppid 456 name vmtoolsd.exe user CLINE\Administrator
pid 180 ppid 456 name rundll32.exe user CLINE\Administrator
pid 1208 ppid 724 name logon.scr user CLINE\Administrator这是数组..。
[*] [{"pid"=>0, "ppid"=>0, "name"=>"[System Process]", "user"=>""}, {"pid"=>456, "ppid"=>320, "name"=>"explorer.exe", "user"=>"CLINE\\Administrator"}, {"pid"=>544, "ppid"=>204, "name"=>"TPAutoConnect.exe", "user"=>"CLINE\\Administrator"}, {"pid"=>1096, "ppid"=>456, "name"=>"vmtoolsd.exe", "user"=>"CLINE\\Administrator"}, {"pid"=>180, "ppid"=>456, "name"=>"rundll32.exe", "user"=>"CLINE\\Administrator"}, {"pid"=>1208, "ppid"=>724, "name"=>"logon.scr", "user"=>"CLINE\\Administrator"}]但是我希望输出像一个表,就像这样
pid ppid name user
1 0 x.exe me
2 1 y.exe you 我的密码里漏掉了什么?
发布于 2014-05-01 02:42:23
您可以使用sprintf打印固定宽度的列。首先获取每个列的最大长度,然后使用sprintf并与"\t"连接所有值:
max_length = filtered.inject({}) {|m,e| e.each{|key,value| m[key] = [key.length, m[key].to_i, value.to_s.length].max}; m}
puts filtered.collect{|e| e.collect{|key,value| sprintf("%#{max_length[key]}s", value)}.join("\t")}更新:要打印出标题,可以使用以下内容
左对齐:
max_length.collect{|key, value| sprintf("%-#{value}s", key)}.join("\t")右对齐:
max_length.collect{|key, value| sprintf("%#{value}s", key)}.join("\t")https://stackoverflow.com/questions/23400990
复制相似问题