需要解析这个dumpsys输出(最好用Perl)

时间:2016-03-04 22:52:32

标签: perl awk sed grep

我有问题。我需要解析下面给出的dumpsys输出:

 Activity Resolver Table:
 Non-Data Actions:
    android.intent.action.MAIN:
    422df0c8.   com.sec.factory.camera/com.sec.android.app.camerafirmware.CameraFirmware filter 422df360
       Action: "android.intent.action.MAIN"
      Category: "android.intent.category.DEFAULT"
    422df610 com.sec.factory.camera/com.sec.android.app.camerafirmware.CameraFirmware_user filter 422b65f0
      Action: "android.intent.action.MAIN"
      Category: "android.intent.category.DEFAULT"
    420d08e8 com.sec.factory.camera/com.sec.android.app.camerafirmware.CameraFirmwareUpdate_Booting filter 420d0ac8
      Action: "android.intent.action.MAIN"
      Category: "android.intent.category.DEFAULT"
  com.sec.android.app.camaftest.SCAN:
    420a2220 com.sec.factory.camera/com.sec.android.app.camaftest.CaptureActivity filter 4209f468
      Action: "com.sec.android.app.camaftest.SCAN"
      Category: "android.intent.category.DEFAULT"

Receiver Resolver Table:
 Schemes:
    android_secret_code:
     422b69b0.   com.sec.factory.camera/com.sec.android.app.camerafirmware.CameraFirmwareBroadCastReceiver filter 422b6b80
       Action:   "android.provider.Telephony.SECRET_CODE"
       Scheme:  "android_secret_code"
      Authority: "34971539": -1
      Authority: "7412365": -1

Non-Data Actions:
    android.intent.action.BOOT_COMPLETED:
     420d0c70. com.sec.factory.camera/com.sec.android.app.camerafirmware.CameraFirmwareBroadCastReceiver filter 420d0d18
       Action: "android.intent.action.BOOT_COMPLETED"

从这个输出(来自android中的一个名为dumpsys的工具),我想获得Non-Data Actions:标题下的内容(我不想,例如,想要内容下的内容) Receiver Resolver Table:标题。

我想在Perl中做这个,但欢迎其他建议。

2 个答案:

答案 0 :(得分:2)

你没有说你是否想要标题本身 - 在这里我假设你确实想要它。这是“Unix过滤器”样式,意味着您需要将转储数据传递到其中,或者提供包含数据的文件名作为脚本的第一个参数。

#!/usr/bin/env perl
use v5.12;

my $target = 'Non-Data \s+ Actions:';
my $taking_data = 0;

while (<>) {
   $taking_data = 1 if (/ ^ \s* $target \s* /x) ;
   print if $taking_data;
   $taking_data = 0 if / ^ \s* $ /x ;
}

像这样:$ cat dump_data.txt | ./android.pl$ ./android.pl dump_data.txt产生:

Non-Data Actions:
    android.intent.action.MAIN:
    422df0c8.   com.sec.factory.camera/com.sec.android.app.camerafirmware.CameraFirmware filter 422df360
       Action: "android.intent.action.MAIN"
      Category: "android.intent.category.DEFAULT"
    422df610 com.sec.factory.camera/com.sec.android.app.camerafirmware.CameraFirmware_user filter 422b65f0
      Action: "android.intent.action.MAIN"
      Category: "android.intent.category.DEFAULT"
    420d08e8 com.sec.factory.camera/com.sec.android.app.camerafirmware.CameraFirmwareUpdate_Booting filter 420d0ac8
      Action: "android.intent.action.MAIN"
      Category: "android.intent.category.DEFAULT"
  com.sec.android.app.camaftest.SCAN:
    420a2220 com.sec.factory.camera/com.sec.android.app.camaftest.CaptureActivity filter 4209f468
      Action: "com.sec.android.app.camaftest.SCAN"
      Category: "android.intent.category.DEFAULT"

Non-Data Actions:
    android.intent.action.BOOT_COMPLETED:
     420d0c70. com.sec.factory.camera/com.sec.android.app.camerafirmware.CameraFirmwareBroadCastReceiver filter 420d0d18
       Action: "android.intent.action.BOOT_COMPLETED"

没有多少;如果我们在一条线上发现目标,可选地由空格包围,则设置一个标志;如果我们看到一个空行,请清除旗帜;在两者之间;如果设置了标志,则打印该行。

正则表达式末尾的'/ x'会在“扩展模式”上切换, 无关紧要 正则表达式中的空白以使布局更清晰。

答案 1 :(得分:0)

根据这些日志的格式,您可以使用sed选择所需的块,例如假设每个块后跟一个空行:

$ sed -n '/Non-Data Actions:/,/^$/p' dumpsys
 Non-Data Actions:
    android.intent.action.MAIN:
    422df0c8.   com.sec.factory.camera/com.sec.android.app.camerafirmware.CameraFirmware filter 422df360
       Action: "android.intent.action.MAIN"
      Category: "android.intent.category.DEFAULT"
    422df610 com.sec.factory.camera/com.sec.android.app.camerafirmware.CameraFirmware_user filter 422b65f0
      Action: "android.intent.action.MAIN"
      Category: "android.intent.category.DEFAULT"
    420d08e8 com.sec.factory.camera/com.sec.android.app.camerafirmware.CameraFirmwareUpdate_Booting filter 420d0ac8
      Action: "android.intent.action.MAIN"
      Category: "android.intent.category.DEFAULT"
  com.sec.android.app.camaftest.SCAN:
    420a2220 com.sec.factory.camera/com.sec.android.app.camaftest.CaptureActivity filter 4209f468
      Action: "com.sec.android.app.camaftest.SCAN"
      Category: "android.intent.category.DEFAULT"

Non-Data Actions:
    android.intent.action.BOOT_COMPLETED:
     420d0c70. com.sec.factory.camera/com.sec.android.app.camerafirmware.CameraFirmwareBroadCastReceiver filter 420d0d18
       Action: "android.intent.action.BOOT_COMPLETED"