Get package name and corr. data from file

1

I've been banging my head lately,trying to parse dumpsys output.

Here is the output:

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
  }

I want to get the package name and the corresponding extras={} for each of them.

For example:

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

So far I've tried:

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

But without any success.

I'm a newbie to awk,and if possible I want to do it with awk or perl.Of course,any other tool like sed or grep is fine by me too,I just wanna parse it somehow. Can anyone help me?

bash
perl
awk
sed
grep
asked on Stack Overflow Mar 1, 2016 by user553637

3 Answers

1

If you have GNU awk, try the following:

awk -v RS='(^|\n)NotificationRecord\\([^=]+=' \
  'NF { print "pkg:" $1; print gensub(/^.*\n\s*(extras=\{[^}]+\}).*$/, "\\1", 1) }' file
  • -v RS='(^|\n)NotificationRecord\\([^=]+=' breaks the input into records by lines starting with NotificationRecord( up to and including the following = char.

    • In effect, that means you get records starting with the package names (com.android.systemui, ...`)
  • NF is a condition that only executes the following block if it evaluates to nonzero; NF is the count of fields in the record, so as long as at least 1 field is present, the block is evaluated - in effect, this skips the implied empty record before the very first line.

  • print "pkg:" $1 prints the package name, prefixed with literal pkg:.

  • gensub(/^.*\n\s*(extras=\{[^}]+\}).*$/, "\\1", 1) matches the entire record and replaces it with the extras property captured via a capture group, effectively returning the extras property only.

answered on Stack Overflow Mar 1, 2016 by mklement0 • edited Mar 1, 2016 by mklement0
0

I would suggest perl over awk, because you'll be storing whether you're inside the extras=... block in a variable:

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

Oh, if you want the extra pkg: and extras= text, slight modification:

dumpsys notification | perl -lne '
    print "pkg: $1" if /^Notif.*?: pkg=(\S+)/;
    $in_extras = 1 if /^  extras=\{/;
    print if $in_extras;
    $in_extras = 0 if /^  \}/;'
answered on Stack Overflow Mar 1, 2016 by user2926055 • edited Mar 1, 2016 by user2926055
0

Sed version:

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

I'm assuming you always have two spaces in front of extras={ and } and you also want to remove these spaces.

answered on Stack Overflow Mar 1, 2016 by Joao Morais

User contributions licensed under CC BY-SA 3.0