2024-04-20 13:03:09 发布
网友
我有一个.txt文件中“adbshelltop”的输出结果。我只想获取我的测试应用程序的CPU使用率并将其显示为表:
Time cpu % 1 18 2 20 3 15 4 19
我试过cat top.txt | grep Cpu | nl > cpu.txt(按照这里的说明link),但是文件是空的。在
cat top.txt | grep Cpu | nl > cpu.txt
你知道我怎么开始吗?我从未使用过regex,也不确定它是否有用。请期待您的建议:)
鉴于您的top.txt示例,我将一步一步地执行以下操作:
top.txt
首先,使用grep只选择应用程序com.test.app的行:
grep
com.test.app
grep com.test.app top.txt
通过管道进入awk,获取第三个字段(由空格分隔,因此这是CPU使用情况):
awk
通过管道将行号添加到nl(其中-nln用于左对齐):
nl
-nln
最后,只需在标题前面加上echo(-e),新行\n需要双引号):
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%
鉴于您的
top.txt
示例,我将一步一步地执行以下操作:首先,使用
grep
只选择应用程序com.test.app
的行:通过管道进入
^{pr2}$awk
,获取第三个字段(由空格分隔,因此这是CPU使用情况):通过管道将行号添加到
^{3}$nl
(其中-nln
用于左对齐):最后,只需在标题前面加上
echo
(-e
),新行\n
需要双引号):这将为您提供示例
top.txt
:相关问题 更多 >
编程相关推荐