Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to have a live preview of a filtered plain text lines. It is important that multiple filters need to be applied either exclusively or inclusively (similar to the filters in process monitor). After filtering is ready, I would like to be able to export the result.

I could achieve similar with grep "filter1\|filter2\|filter3 but some of my greps are buggy and it is hard to maintain this kind of filters there are plenty filters. Live preview is not easily achieved either, because | head is only a few lines and with | less where I can do text search, I'm not sure if I can save the result. (Also it's a bit inconvenient with pipeline, even if I like command line.)

For a different user experience, I could use something more visual (although prefer minimal).

share|improve this question

2 Answers 2

Look what I made for you, cute little naxa :)

running on /proc/cpuinfo

The above is the result of running python3 live-line-filter.py /proc/cpuinfo with the filters you see in the screenshot above.

Reading from stdin would be nice, but it's hard because Qt does not seem to support it. I guess you could do it in plain Python, run the Qt app in another thread, and have Qt listen on a socket (and feed it from the Python main thread)... Too much work for a tiny improvement. I guess you already have your data if you want to try live filters.

On Ubuntu, you can install python3-pyqt4 to get PyQt (which is needed).

live-line-filter.py:

from PyQt4 import Qt
import sys
import fileinput
import re

class LiveLineFilterApplication(Qt.QApplication):

    def __init__(self, args):
        Qt.QApplication.__init__(self, args)
        self.addWidgets()

        self.unfilteredlines = [x for x in fileinput.input()]

        self.slotRefilter()

        self.exec_()

    def addWidgets(self):
        self.w = Qt.QWidget()
        self.layout = Qt.QVBoxLayout(self.w)

        self.input = Qt.QTextEdit(".*", None)
        self.input.textChanged.connect(self.slotRefilter)

        self.textedit = Qt.QTextEdit("pending", None)
        self.textedit.setReadOnly(True)

        self.layout.addWidget(self.input)
        self.layout.addWidget(self.textedit)
        self.w.show()

    def slotRefilter(self):
        filters = self.input.toPlainText().strip()
        matches = self.unfilteredlines
        for filt in filters.split("\n"):
          if filt == "": continue
          r = re.compile(filt)
          matches = [x for x in matches if r.search(x)]
        self.textedit.setText("".join(matches))

if __name__ == "__main__":
    app = LiveLineFilterApplication(sys.argv)

Alternatives:

share|improve this answer
    
wow thanks! do we know each other? ;P –  naxa Jun 10 '13 at 18:08
    
@naxa: no, i was just trying to freak you out :P. Tell me if the program works for you. –  Janus Troelsen Jun 10 '13 at 18:17

Software requests are off topic here. However, as a workaround, you could use grep's -f flag:

   -f FILE, --file=FILE
          Obtain patterns from FILE, one per line.   The
          empty   file   contains   zero  patterns,  and
          therefore matches nothing.  (-f  is  specified
          by POSIX.)
share|improve this answer
    
didn't know it's off-topic. there is a tag software-rec. => ??? btw thanks for noting! –  naxa Jun 9 '13 at 14:15
    
@naxa Yes, there's a tag but tags can be created by anyone, see here, this falls under "product recommendation". –  terdon Jun 9 '13 at 14:17
    
@terdon: No, not anyone. You need 300 rep. –  Janus Troelsen Jun 9 '13 at 14:18
1  
@naxa it's on topic for questions about a particular problem (read: anything but "What program can do this?"). From the software-rec tag: Important: It's often better to ask for a real problem instead of a software to solve it. So in this case, it would have been better to simply ask how it's possible to combine regular expressions, or inquire as to how the grep tool could be used to stack filters (the general ideology of that still falls under the general UNIX philosophy). –  Breakthrough Jun 9 '13 at 14:38
    
I see, thank you very much! I seek user experience here, not mere 'functionality', since it would add meaningful value for me. (Thinking loudly : this should be valid right, since otherwise I would only need cat and gcc, not even grep). I better word it torwards the user experience then. –  naxa Jun 9 '13 at 14:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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