1

I'm currently using grep in combination with adb logcat (android console api). Now I want to edit every line on the fly using sed in a pipe. However when using sed after grep the output becomes empty. Does anyone know why? Also: adb logcat keeps on running with grep which might be part of the issue, however I don't know how to fix it

here is an example

$ adb logcat | grep "Average inference"

gives me

04-16 13:51:19.528  8065  8065 E tflite  : Average inference timings in us: Warmup: 38350.1, Init: 2158247, Inference: 70339.2Overall max resident set size = 65.7773 MB, total malloc-ed size = 0 MB, in-use allocated/mmapped size = 10.3201 MB  
04-16 13:51:19.528  8065  8065 E tflite  : Average inference timings in us: Warmup: 38350.1, Init: 2158247, Inference: 70339.2Overall max resident set size = 65.7773 MB, total malloc-ed size = 0 MB, in-use allocated/mmapped size = 10.3201 MB  

Now I want to use this pipe to extract all numbers (after "Warmup")

sed -e 's/^.*Warmup//' -e 's/[^0-9.0-9]/ /g' 

to get the following result

38350.1 2158247 70339.2 65.7773 0 10.3201
38350.1 2158247 70339.2 65.7773 0 10.3201

however

adb logcat | grep "Average inference" | sed -e 's/^.*Warmup//' -e 's/[^0-9.0-9]/ /g' 

only gives me an empty output

Update:

In general adb logcat gives me many arbitrary outputs which I would need to suppress. So a typical ouput would look like

05-04 20:18:00.063  5670  6838 I LiveIconUtil: mIconDpi : 480 , mTargetIconDpi : 240
05-04 20:18:00.081  5670  6838 I AppIconSolution: load= live icon for com.sec.android.app.clockpackage, from overlay = false
05-04 20:18:00.082  5670  6838 I LauncherActivityInfo: Load live icon for com.sec.android.app.clockpackage
05-04 20:18:00.082  5670  6838 I LauncherActivityInfo: packageName: com.sec.android.app.clockpackage, useThemeIcon: false, height: 144, width: 144, density: 480
05-04 20:18:00.103  5670  5670 E libprocessgroup: set_timerslack_ns write failed: Operation not permitted
05-04 20:18:00.138  5270  5270 W GraphicUtils: getTextEmpty : skipped with null text
05-04 20:18:00.138  5270  5270 W GraphicUtils: setLinearGradientTextColor : skipped with null text
05-04 20:18:03.232  4462  4462 I SurfaceFlinger: SFWD update time=991188773612812
04-16 13:51:19.528  8065  8065 E tflite  : Average inference timings in us: Warmup: 38350.1, Init: 2158247, Inference: 70339.2Overall max resident set size = 65.7773 MB, total malloc-ed size = 0 MB, in-use allocated/mmapped size = 10.3201 MB  
04-16 13:51:19.528  8065  8065 E tflite  : Average inference timings in us: Warmup: 38350.1, Init: 2158247, Inference: 70339.2Overall max resident set size = 65.7773 MB, total malloc-ed size = 0 MB, in-use allocated/mmapped size = 10.3201 MB  
...

So I would still somehow need to grep the "Average inference" lines, only with using sed instead as suggested by @FritjofLarsson

1

This should do (no need for grep):

#!/bin/sh

adb logcat | sed -n '
/Average inference/ {
    s/.*Warmup: //
    s/[^0-9]\{2,\}/ /g
    # Optional: remove the trailing space.
    s/ $//
    p
}
'

/Average inference/ will only match lines containing 'Average inference'.
s/.*Warmup: // makes sed discard anything before the first occurrence of 'Warmup: '.
s/[^0-9]\{2,\}/ /g will replace every two or more consecutive non-digit characters with a space. The decimal point will be preserved since it is surrounded by digits.
s/ $// will trim the trailing space introduced by s/[^0-9]\{2,\}/ /g when ' MB' was replaced in the end of the line.
p is used in combination with -n flag in order to make sed only output the modified lines.

Note: You may write the sed command in one line like this:
sed -n '/Average inference/{s/.*Warmup: //;s/[^0-9]\{2,\}/ /g;s/ $//;p}'

2
  • apparently this still outputs all logcat lines. Maybe I need to delete all "non matching lines" first? Sorry the minimal example above was too minimal. I'll update it. – mcExchange 2 days ago
  • 1
    Your minimal example was enough. It's my mistake, I forgot that sed (unlike awk) prints all input lines by default, unless -n is passed. I've updated my answer. – Fritjof Larsson 2 days ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.