简体   繁体   中英

Optimize sed on logcat

I've been using grep and sed on some logcat output to make it more readable and I noticed my output was noticeably slower than just grep -ing the output.

I understand sed is obviously going to add more runtime, but I wanted to check for any optimization techniques.

My commands look something like this for reference:

adb logcat | grep arg | sed $'s/{/\\\\\\n{/g

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' 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 .* , ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM