获取包名称和corr。来自文件的数据

时间:2016-03-01 00:10:00

标签: bash perl awk sed grep

我最近一直在试图解析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
  }

我想获取包名称和相应的附加内容= {} 对于他们每个人。

例如:

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

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

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

但没有任何成功。

我是awk的新手,如果可能的话,我想用awk或perl来做。当然,像sed或grep这样的任何其他工具对我来说都很好,我只想以某种方式解析它。 任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

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

awk -v RS='(^|\n)NotificationRecord\\([^=]+=' \
  'NF { print "pkg:" $1; print gensub(/^.*\n\s*(extras=\{[^}]+\}).*$/, "\\1", 1) }' file
  • -v RS='(^|\n)NotificationRecord\\([^=]+='将输入分为以NotificationRecord(开头的行,直至并包含以下=字符。

    • 实际上,这意味着您将获得以包名称开头的记录(com.android.systemui,...`)
  • NF是一个条件,如果它的计算结果为非零,则只执行以下块; NF是记录中字段的数量,因此只要存在至少1个字段,就会对该块进行评估 - 实际上,这会在第一行之前跳过隐含的空记录。

    < / LI>
  • print "pkg:" $1打印包名,前缀为文字pkg:

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

答案 1 :(得分:0)

我建议perl over 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 /^  \}/;'

答案 2 :(得分:0)

Sed版本:

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

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