Important: Please read the Qt Code of Conduct -

Running GREP from QProcess (pyqt4)

  • Hello,

    I just can't get this to run correctly. I am trying to run the grep via QProcess, as follows:

    self.process = QtCore.QProcess()
    self.arguments_test = QtCore.QStringList()

    self.arguments_test << "-rn" << "hello" << "/home/user/*";
    self.process.start("grep", self.arguments_test)

    I get : <PyQt4.QtCore.QStringList object at 0xb4776df4>

    I've tried running simpler commands such as ls, with parameters too and it works.

    Thanks in advance.

  • QProcess won't do shell wildcard expansion for you. Invoke a shell (sh -c or such) to perform that.

  • Hello pepe,

    Thanks for replying. So, do you have any basic code sample for invoking the shell? What is the class I should be looking at?


  • QProcess is the right class :) you just need to invoke a shell to perform that wildcard expansion.
    Something like
    QStringList options;
    options << "-c" << "grep -rn hello /tmp/*";
    QProcess process;
    process.start("/bin/sh", options);

  • I will try that! thanks a lot for your help.

  • Hi dangelog your code works perfectly.A ton of thaks to you.But i am searching for a way to provide the search pattern name in a variable so that the user can give any pattern to search

    QString pattern = "hello";
    options << "-c" << "grep -r -w -i -o -c "<<pattern<<" hello /tmp/* ";

    doesn't work...

Log in to reply