PUBLIC OBJECT

Rip.java

Rip.java is a grep-style tool I've been using since 2008. I'm not old-school enough for 'sed & awk', but can get a lot done with Rip. Here's how it works...

It starts when you have some text, like from a log or command or search. Perhaps it's logcat output:

I/RDIO    (20545): AndroidSession.BG: Network available: WIFI
D/PicasaSyncManager(22054): active network: NetworkInfo: type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: "65 Ridgeview", roaming: false, failover: false, isAvailable: true
D/dalvikvm( 1612): GC_CONCURRENT freed 443K, 6% free 9187K/9720K, paused 53ms+3ms, total 144ms
D/dalvikvm( 1612): WAIT_FOR_CONCURRENT_GC blocked 79ms
D/dalvikvm( 1745): GC_CONCURRENT freed 429K, 6% free 9603K/10148K, paused 56ms+4ms, total 128ms
D/dalvikvm( 1745): WAIT_FOR_CONCURRENT_GC blocked 71ms
D/dalvikvm( 1612): GC_FOR_ALLOC freed 394K, 6% free 9179K/9720K, paused 27ms, total 28ms
V/GameCenter - UALib( 1612): Detected active IP address as: fe80::a00b:baff:fec6:ea88%p2p0
D/dalvikvm(22232): GC_CONCURRENT freed 358K, 4% free 9474K/9864K, paused 4ms+4ms, total 30ms
D/dalvikvm(22232): GC_CONCURRENT freed 438K, 5% free 9504K/9976K, paused 5ms+35ms, total 71ms

Suppose I'd like to extract out the GC_CONCURRENT times so I can paste them into a spreadsheet.

  1. Write a regular expression that captures the lines I'm interested in: GC_CONCURRENT freed \d+K, \d+% free \d+K/\d+K, paused \d+ms\+\d+ms, total \d+ms. This part is like grep, but with Java's regex engine behind-the-scenes.

  2. Add ( and ) for capturing groups. Here I want the amount of memory freed and total time: GC_CONCURRENT freed (\d+)K, \d+% free \d+K/\d+K, paused \d+ms\+\d+ms, total (\d+)ms.

  3. Create a format string that echoes back the matches. Use %s for the full match, %2$s for the first capture group, %3$s for the second group, etc. For my spreadsheet I want "time, freed" so I'll use %3$s, %2$s.

  4. Run it. Rip requires 2 arguments: the regex and the format string. I use single quotes so the shell doesn't try to escape anything.

$ *adb logcat | Rip.java -s 'GC_CONCURRENT freed (\d+)K, \d+% free \d+K/\d+K, paused \d+ms\+\d+ms, total (\d+)ms' '%3$s, %2$s'*
58, 456
33, 601
32, 443
30, 465
32, 455
36, 470
...

Note 1: Rip.java uses an overly clever, filthy hack so you can execute it directly from bash. If you download Rip.java, you'll need to chmod u+x the file.

Note 2: Above I pass -s to skip input lines that don't match the regex. Otherwise Rip complains, which is useful if you expect all lines to match.