1

windows 10, Emacs 26.1

M-x shell

enter image description here

I start Android application that very long time generate many lines in shell.

adb logcat -vtime

Here example:

enter image description here

In all of this text I need only lines that contain this text:

com.example.myprojectname

I need to filter this "on fly" interactively. It's very important. So the result must be.

  1. M-x shell
  2. Start my Android app
  3. On shell show ONLY lines that contains text com.example.myprojectname
  4. This MUST be happen "on fly" interactively

Is it possible?

2 Answers 2

4

If you don't have a grep available, you can use comint-preoutput-filter-functions to do the filtering for you. You need to arrange for that variable to have the right value in shell-mode, which can be done using add-hook. Since shell-mode adds its own functions to that variable already, you need to perform the addition in the shell-mode hook function. Something like the following, which assumes your prompt starts with $ or ~

(defun my-comint-filter (str)
  (if (string-match "^\\$\\|^\\~\\|com\\.example\\.myprojectname" str)
      str
    ""))
(defun my-shell-mode-hook ()
    (add-hook 'comint-preoutput-filter-functions 'my-comint-filter t t))
(add-hook 'shell-mode-hook 'my-shell-mode-hook)
2

can you do the plain unix pipe trick of:

  1. M-x shell
  2. myAndroidApp | grep --line-buffered -i com.example.myprojectname

that is what I do. Or are there special requirements?

as others have mentioned. The text after the grep command is a regular expression (the -i is a flag for case insensitive matching). To get the pattern to match you may need to escape characters. The escape mechanisms for bash (unix, linux, Mac etc) are different from MS Windows. The escapes for Window's CMD are here: https://ss64.com/nt/syntax-esc.html. If you search for how to use grep, most of the examples will be for unix's bash. Just a heads up.

8
  • I'm unsure what the current state of the the Linux Bash shell for Windows is, but @Alexei may need to enable it.
    – slackline
    Sep 12, 2018 at 11:03
  • @slackline, oh good point. I have not used Windows in years. If the new embedded linux kernel is not enabled (I heard it works well), in the past, when on Windows, I used: gnuwin32.sourceforge.net/packages/grep.htm. with great success. Sep 12, 2018 at 11:08
  • @JamesAnderson I has Git, as result I has grep. But myAndroidApp | grep -i com.example.myprojectname not work correct. It start good, but then stop show anything. Sep 12, 2018 at 12:43
  • I updated my post Sep 12, 2018 at 12:47
  • @JamesAnderson But if I start this: myAndroidApp | grep -i com.example.myprojectname on Windows console (cmd.exe) than work FINE. It's success filtered "on fly". Sep 12, 2018 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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