我正在尝试编写一个脚本,它将获取PID和CPU值,但我面临着sapce的问题。
以下是我正在处理的日志:
5899 0 0% D 1 0K 0K root kworker/u:4
6213 1 0% S 1 0K 0K root migration/1
6214 1 0% S 1 0K 0K root kworker/1:0
6215 1 0% S 1 0K 0K root ksoftirqd/1
6824 1 0% S 1 848K 448K root /system/bin/
6837 2 0% S 1 0K 0K root migration/2
6838 2 0% S 1 0K 0K root kworker/2:0
6839 2 0% S 1 0K 0K root ksoftirqd/2
6840 3 0% S 1 0K 0K root migration/3
6841 3 0% S 1 0K 0K root kworker/3:0
6842 3 0% S 1 0K 0K root ksoftirqd/3
10659 2 0% S 15 297136K 22572K bg u0_a15 android.proc
10882 0 0% S 13 294316K 22636K bg u0_a23 com.google.a
12473 1 0% S 13 287420K 19936K bg u0_a0 com.android.
12612 0 0% S 5 4504K 240K root /sbin/adbd
12614 1 0% S 1 0K 0K root k_rmnet_work
13429 0 0% S 28 307800K 22312K bg u0_a19 com.google.a
13501 0 0% S 11 282628K 19212K bg u0_a55 com.qrd.simc
13867 3 0% S 16 297644K 27700K bg u0_a37 com.android.
13927 0 0% S 10 281604K 19552K fg u0_a38 com.android.
14057 0 0% S 10 283148K 19224K bg u0_a72 com.asiainfo
14983 2 0% S 14 291744K 21596K bg u0_a92 com.baidu.Ba
15647 2 0% S 14 293192K 27800K bg u0_a21 com.android.
20206 1 0% S 1 0K 0K root kworker/u:3
32229 0 0% S 1 0K 0K root kworker/0:1
81 1 0% S 1 0K 0K root dalrpc_rcv_D
2 0 0% S 1 0K 0K root kthreadd
3 0 0% S 1 0K 0K root ksoftirqd/0
6 0 0% S 1 0K 0K root migration/0
在上面的日志中,我试图捕获第一列和第三列值。
这是我的代码:
$ outval_1是标量变量我在某些命令的帮助下占用每一行。
my @text_words_1 = split(/\s+/, $outval_1);
#print "@text_words_1"
foreach my $val(@text_words_1)
{
if($val =~/^\s+/)
{
print "Sapce is there\n";
}
else
{
print "$val\n";
}
}
答案 0 :(得分:3)
你的一些行以空格开头,所以在拆分之前你可以删除它们,
$outval_1 =~ s/^ \s+ //x;
这样$text_words_1[0]
始终是第一列的值。