The useless grep
is well-documented and easy to get rid of.
adb logcat | sed $'/\\*arg/s/{/\\\n{/g'
To briefly reiterate the linked web page, anything that looks like grep 'x' | sed 'y'
can be refactored to sed '/x/y'
(and similarly for grep 'x' | awk 'y'
, which reduces to awk '/x/ y'
). sed
and Awk are both generalized regex tools which can do everything grep
can do (though in fairness some complex grep
options are tedious to reimplement in a sed
or Awk script; but this is obviously not one of these cases).
However, *arg*
is not a well-defined regex; so I have to guess what you actually mean.
*
at the beginning of a regex isn't well-defined; but many grep
implementations will understand it to mean a literal asterisk. If that's not what you meant, probably take away the first \\*
.
arg*
is exactly equivalent to ar
; if you don't care whether there are g
characters after the match, just don't specify them. But perhaps you actually meant arg
followed by anything?
- But then I guess you probably meant just
arg
(implicitly preceded by and followed by anything).
In case it's not already obvious, *
is not a wildcard character in regex. Instead, it says to repeat the preceding expression as many times as possible, zero or more (and thus the way to say "any string at all" in regex is .*
, i.e. the "any character (except newline)" wildcard character .
repeated zero or more times).
Also, grep
(and sed
, and Awk) look for the regex anywhere in a line (unless you put in explicit regex anchors or use grep -x
or equivalent options in sed
or Awk) so you don't need to specify "preceded by anything" or "followed by anything".
The Bash "C-style string" $'...'
offers some conveniences, but also requires any literal backslash to be doubled. So $'/\\*/'
is equivalent to '/\*/'
in regular single quotes.
The reason the sed
slows you down is probably buffering, but getting rid of the useless grep
also coincidentally gets rid of that buffering.