【问题标题】:Get package name and corr. data from file获取包名和更正。文件中的数据
【发布时间】:2016-03-01 00:09:59
【问题描述】:

我最近一直在敲我的头,试图解析 dumpsys 输出。

这是输出:

NotificationRecord(0x4297d448: pkg=com.android.systemui user=UserHandle{0} id=273 tag=null score=0: Notification(pri=0 icon=7f020148 contentView=com.android.systemui/0x1090069 vibrate=null sound=null defaults=0x0 flags=0x2 when=0 ledARGB=0x0 contentIntent=N deleteIntent=N contentTitle=6 contentText=15 tickerText=6 kind=[null]))
  uid=10012 userId=0
  icon=0x7f020148 / com.android.systemui:drawable/stat_sys_no_sim
  pri=0 score=0
  contentIntent=null
  deleteIntent=null
  tickerText=No SIM
  contentView=android.widget.RemoteViews@429c1f58
  defaults=0x00000000 flags=0x00000002
  sound=null
  vibrate=null
  led=0x00000000 onMs=0 offMs=0
  extras={
    android.title=No SIM
    android.subText=null
    android.showChronometer=false
    android.icon=2130837832
    android.text=Insert SIM card
    android.progress=0
    android.progressMax=0
    android.showWhen=true
    android.infoText=null
    android.progressIndeterminate=false
    android.scoreModified=false
  }
NotificationRecord(0x427e1878: pkg=jackpal.androidterm user=UserHandle{0} id=1 tag=null score=0: Notification(pri=0 icon=7f02000d contentView=jackpal.androidterm/0x1090069 vibrate=null sound=null defaults=0x0 flags=0x62 when=1456782124817 ledARGB=0x0 contentIntent=Y deleteIntent=N contentTitle=17 contentText=27 tickerText=27 kind=[null]))
  uid=10094 userId=0
  icon=0x7f02000d / jackpal.androidterm:drawable/ic_stat_service_notification_icon
  pri=0 score=0
  contentIntent=PendingIntent{42754f78: PendingIntentRecord{42802aa0 jackpal.androidterm startActivity}}
  deleteIntent=null
  tickerText=Terminal session is running
  contentView=android.widget.RemoteViews@4279b510
  defaults=0x00000000 flags=0x00000062
  sound=null
  vibrate=null
  led=0x00000000 onMs=0 offMs=0
  extras={
    android.title=Terminal Emulator
    android.subText=null
    android.showChronometer=false
    android.icon=2130837517
    android.text=Terminal session is running
    android.progress=0
    android.progressMax=0
    android.showWhen=true
    android.infoText=null
    android.progressIndeterminate=false
    android.scoreModified=false
  }
NotificationRecord(0x429381f8: pkg=com.droidsail.dsapp2sd user=UserHandle{0} id=128 tag=null score=0: Notification(pri=0 icon=7f020000 contentView=com.droidsail.dsapp2sd/0x1090069 vibrate=null sound=null defaults=0x0 flags=0x10 when=1456786729004 ledARGB=0x0 contentIntent=Y deleteIntent=N contentTitle=13 contentText=35 tickerText=35 kind=[null]))
  uid=10107 userId=0
  icon=0x7f020000 / com.droidsail.dsapp2sd:drawable/appicon
  pri=0 score=0
  contentIntent=PendingIntent{42955a60: PendingIntentRecord{4286db18 com.droidsail.dsapp2sd startActivity}}
  deleteIntent=null
  tickerText=Detected new app can be moved to SD
  contentView=android.widget.RemoteViews@42a891a8
  defaults=0x00000000 flags=0x00000010
  sound=null
  vibrate=null
  led=0x00000000 onMs=0 offMs=0
  extras={
    android.title=New app to SD
    android.subText=null
    android.showChronometer=false
    android.icon=2130837504
    android.text=Detected new app can be moved to SD
    android.progress=0
    android.progressMax=0
    android.showWhen=true
    android.infoText=null
    android.progressIndeterminate=false
    android.scoreModified=false
  }
NotificationRecord(0x423708b0: pkg=android user=UserHandle{-1} id=17041135 tag=null score=0: Notification(pri=0 icon=1080399 contentView=android/0x1090069 vibrate=null sound=null defaults=0x0 flags=0x1002 when=0 ledARGB=0x0 contentIntent=Y deleteIntent=N contentTitle=19 contentText=17 tickerText=N kind=[android.system.imeswitcher]))
  uid=1000 userId=-1
  icon=0x1080399 / android:drawable/ic_notification_ime_default
  pri=0 score=0
  contentIntent=PendingIntent{425a8960: PendingIntentRecord{426f84b0 android broadcastIntent}}
  deleteIntent=null
  tickerText=null
  contentView=android.widget.RemoteViews@428846b8
  defaults=0x00000000 flags=0x00001002
  sound=null
  vibrate=null
  led=0x00000000 onMs=0 offMs=0
  extras={
    android.title=Choose input method
    android.subText=null
    android.showChronometer=false
    android.icon=17302425
    android.text=Hacker's Keyboard
    android.progress=0
    android.progressMax=0
    android.showWhen=true
    android.infoText=null
    android.progressIndeterminate=false
    android.scoreModified=false
  }

我想获取包名和对应的 extras={} 他们每个人。

例如:

pkg:com.android.systemui
extras={
  .....
 }

到目前为止我已经尝试过:

dumpsys notification | awk '/pkg=/,/\n}/'

但没有任何成功。

我是 awk 的新手,如果可能的话,我想用 awk 或 perl 来做。当然,我也可以使用 sed 或 grep 等任何其他工具,我只是想以某种方式解析它。 谁能帮帮我?

【问题讨论】:

    标签: bash perl awk sed grep


    【解决方案1】:

    如果您有 GNU awk,请尝试以下操作:

    awk -v RS='(^|\n)NotificationRecord\\([^=]+=' \
      'NF { print "pkg:" $1; print gensub(/^.*\n\s*(extras=\{[^}]+\}).*$/, "\\1", 1) }' file
    
      • 实际上,这意味着您可以获取以包名开头的记录(com.android.systemui,...`)
    • NF 是一个条件,如果它的计算结果为非零,则仅执行以下块; NF 是记录中字段的计数,因此只要至少存在 1 个字段,就会评估块 - 实际上,这会跳过第一行之前隐含的空记录。

    • print "pkg:" $1 打印包名,前缀为 pkg:

    • gensub(/^.*\n\s*(extras=\{[^}]+\}).*$/, "\\1", 1) 匹配整个记录并将其替换为通过捕获组捕获的extras 属性,有效地仅返回extras 属性。

    【讨论】:

      【解决方案2】:

      我建议使用 perl 而不是 awk,因为您将在变量中存储您是否在 extras=... 块内:

      dumpsys notification | perl -lne '
          print $1 if /^Notif.*?: pkg=(\S+)/;
          $in_extras = 0 if /^  \}/;
          print if $in_extras;
          $in_extras = 1 if /^  extras=\{/'
      

      哦,如果你想要额外的 pkg:extras= 文字,稍微修改一下:

      dumpsys notification | perl -lne '
          print "pkg: $1" if /^Notif.*?: pkg=(\S+)/;
          $in_extras = 1 if /^  extras=\{/;
          print if $in_extras;
          $in_extras = 0 if /^  \}/;'
      

      【讨论】:

      • 没有输出,在我执行代码后它只是空白
      • 糟糕,第二个版本有语法错误(现已修复),但第一个版本应该可以正常工作。也许尝试将所有 5 行合二为一?您是否将整个内容复制粘贴为单个命令?
      • 另外,dumpsys notification 正在为 perl 生成输出以供查看,对吗?我不熟悉dumpsys,但可能连续多次重新运行它会产生不同(或零)的输出?也许将dumpsys 重定向到一个文件以确保它正在做某事? dumpsys notification > /tmp/output.txt; cat /tmp/output.txt; cat output.txt | perl -lne '...'
      • 现在可以用了,谢谢!! :) 实际上我忘了告诉你,因此延迟了。只需要进行一些更改,但你的例子启发了我。再次感谢 :)
      【解决方案3】:

      Sed 版本:

      dumpsys notification |\
        sed -n 's/.*pkg=\([^ ]*\).*/pkg:\1/p;/^  extras={$/,/^  }$/s/^  //p'
      

      我假设您总是在 extras={} 前面有两个空格,并且您还想删除这些空格。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-15
        • 1970-01-01
        • 2011-12-14
        • 2014-07-14
        • 2013-11-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多