如何解析“adbshelltop”以仅显示CPU或内存使用情况?

2024-04-20 13:03:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个.txt文件中“adbshelltop”的输出结果。我只想获取我的测试应用程序的CPU使用率并将其显示为表:

Time cpu %

1    18   
2    20 
3    15 
4    19 

我试过cat top.txt | grep Cpu | nl > cpu.txt(按照这里的说明link),但是文件是空的。在

你知道我怎么开始吗?我从未使用过regex,也不确定它是否有用。请期待您的建议:)

^{pr2}$

Tags: 文件txt应用程序timetopnllinkcpu
1条回答
网友
1楼 · 发布于 2024-04-20 13:03:09

鉴于您的top.txt示例,我将一步一步地执行以下操作:

首先,使用grep只选择应用程序com.test.app的行:

grep com.test.app top.txt

通过管道进入awk,获取第三个字段(由空格分隔,因此这是CPU使用情况):

^{pr2}$

通过管道将行号添加到nl(其中-nln用于左对齐):

^{3}$

最后,只需在标题前面加上echo-e),新行\n需要双引号):

echo -e "Time    cpu %\n" && grep com.test.app top.txt | awk '{ print $3; }' | nl -nln

这将为您提供示例top.txt

Time    cpu %

1       20%

相关问题 更多 >