繁体   English   中英

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

[英]How to parse “adb shell top” to display only CPU or memory usage?

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

Time cpu %

1    18   
2    20 
3    15 
4    19 

我试过cat top.txt | grep Cpu | nl > cpu.txt cat top.txt | grep Cpu | nl > cpu.txt cat top.txt | grep Cpu | nl > cpu.txt (按照此处链接的说明),但文件为空。

你知道我怎么能开始吗? 我从来没有使用正则表达式,也不确定它会有所帮助。 请期待听到任何提示:)

    User 23%, System 5%, IOW 0%, IRQ 0%
    User 434 + Nice 0 + Sys 106 + Idle 1273 + IOW 10 + IRQ 0 + SIRQ 3 = 1826

     PID PR CPU% S  #THR     VSS     RSS PCY UID      Name
     1868  1  20% S    50 1394620K 175176K  fg u0_a174  com.test.app
    1841  6   2% S    11 1897824K  48632K  fg shell    uiautomator
    4794  1   1% S   148 2334708K 130924K  fg system   system_server
     447  1   1% S    14 422152K  17476K  fg system /system/bin/surfaceflinger
   29195  1   1% S    21 369676K  20040K  fg media    

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

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

grep com.test.app top.txt

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

grep com.test.app top.txt | awk '{ print $3; }'

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

grep com.test.app top.txt | awk '{ print $3; }' | nl -nln

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

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

这将为您提供示例top.txt

Time    cpu %

1       20%

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM