Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Escape character '\' on an string that is passed to QProcess.start()
Forum Updated to NodeBB v4.3 + New Features

Escape character '\' on an string that is passed to QProcess.start()

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
7 Posts 4 Posters 2.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kate_Software
    wrote on last edited by
    #1

    Hello all,

    I am trying to use QProcess.start() and then sed command to replace a word on a file in Linux system.
    The string of the command needs to have escape character on it '\' . But this is special character in C, so it doesn't recognize it, it just ignores it.
    I have tried to use double '\' but it doesn't work either - this normally works in any C/C++ application but not in Qt.
    Any ideas?
    Thanks.

    raven-worxR 1 Reply Last reply
    0
    • K Kate_Software

      Hello all,

      I am trying to use QProcess.start() and then sed command to replace a word on a file in Linux system.
      The string of the command needs to have escape character on it '\' . But this is special character in C, so it doesn't recognize it, it just ignores it.
      I have tried to use double '\' but it doesn't work either - this normally works in any C/C++ application but not in Qt.
      Any ideas?
      Thanks.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Kate_Software
      how exactly did you use it?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • healermagnusH Offline
        healermagnusH Offline
        healermagnus
        wrote on last edited by
        #3

        If you can post some code, that would help.

        Escaping strings for QProcess can be tricky, which is why QProcess::setArguments() or similar from a QStringList is useful. Sometimes (if you need pipes, semi-colons, or &&, ||) you may want to use "bash -c" as a first command, and the rest as a single escaped command, but seeing your code may answer that.

        1 Reply Last reply
        2
        • K Offline
          K Offline
          Kate_Software
          wrote on last edited by
          #4

          Hi both,
          Thanks for answering. The code is as follows: (See the double escape)
          QProcess myprocess;
          myprocess.setArguments(QStringList() << QStringLiteral("-i") << QStringLiteral("1 s/(OFF\\|FATAL\\|ERROR\\|WARN\\|INFO\\|DEBUG\\|TRACE\\|ALL\\)/ERROR/") << QStringLiteral("/opt/himalayas/eclipse.log.config"));

          K 1 Reply Last reply
          0
          • K Kate_Software

            Hi both,
            Thanks for answering. The code is as follows: (See the double escape)
            QProcess myprocess;
            myprocess.setArguments(QStringList() << QStringLiteral("-i") << QStringLiteral("1 s/(OFF\\|FATAL\\|ERROR\\|WARN\\|INFO\\|DEBUG\\|TRACE\\|ALL\\)/ERROR/") << QStringLiteral("/opt/himalayas/eclipse.log.config"));

            K Offline
            K Offline
            Kate_Software
            wrote on last edited by
            #5

            @Kate_Software said in Escape character '\' on an string that is passed to QProcess.start():

            Hi both,
            Thanks for answering. The code is as follows: (See the double escape)
            QProcess myprocess;
            myprocess.setArguments(QStringList() << QStringLiteral("-i") << QStringLiteral("1 s/(OFF\\|FATAL\\|ERROR\\|WARN\\|INFO\\|DEBUG\\|TRACE\\|ALL\\)/ERROR/") << QStringLiteral("/opt/himalayas/eclipse.log.config"));

            I forgot to add this line -> myprocess.setProgram("sed");
            So the final code is like:

            QProcess myprocess;
            myprocess.setProgram("sed");
            myprocess.setArguments(QStringList() << QStringLiteral("-i") << QStringLiteral("1 s/(OFF\\|FATAL\\|ERROR\\|WARN\\|INFO\\|DEBUG\\|TRACE\\|ALL\\)/ERROR/") << QStringLiteral("/opt/project/app.log.config"));

            raven-worxR JonBJ 2 Replies Last reply
            0
            • K Kate_Software

              @Kate_Software said in Escape character '\' on an string that is passed to QProcess.start():

              Hi both,
              Thanks for answering. The code is as follows: (See the double escape)
              QProcess myprocess;
              myprocess.setArguments(QStringList() << QStringLiteral("-i") << QStringLiteral("1 s/(OFF\\|FATAL\\|ERROR\\|WARN\\|INFO\\|DEBUG\\|TRACE\\|ALL\\)/ERROR/") << QStringLiteral("/opt/himalayas/eclipse.log.config"));

              I forgot to add this line -> myprocess.setProgram("sed");
              So the final code is like:

              QProcess myprocess;
              myprocess.setProgram("sed");
              myprocess.setArguments(QStringList() << QStringLiteral("-i") << QStringLiteral("1 s/(OFF\\|FATAL\\|ERROR\\|WARN\\|INFO\\|DEBUG\\|TRACE\\|ALL\\)/ERROR/") << QStringLiteral("/opt/project/app.log.config"));

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by raven-worx
              #6

              @Kate_Software
              shouldn't the following already be sufficient?

              myprocess.setArguments(QStringList() << QStringLiteral("-i") /* whats 
               with the "1" here? */ << QStringLiteral("'s/(OFF|FATAL|ERROR|WARN|INFO|DEBUG|TRACE|ALL)/ERROR/'") << QStringLiteral("/opt/project/app.log.config")); // note the single quotes
              

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              1
              • K Kate_Software

                @Kate_Software said in Escape character '\' on an string that is passed to QProcess.start():

                Hi both,
                Thanks for answering. The code is as follows: (See the double escape)
                QProcess myprocess;
                myprocess.setArguments(QStringList() << QStringLiteral("-i") << QStringLiteral("1 s/(OFF\\|FATAL\\|ERROR\\|WARN\\|INFO\\|DEBUG\\|TRACE\\|ALL\\)/ERROR/") << QStringLiteral("/opt/himalayas/eclipse.log.config"));

                I forgot to add this line -> myprocess.setProgram("sed");
                So the final code is like:

                QProcess myprocess;
                myprocess.setProgram("sed");
                myprocess.setArguments(QStringList() << QStringLiteral("-i") << QStringLiteral("1 s/(OFF\\|FATAL\\|ERROR\\|WARN\\|INFO\\|DEBUG\\|TRACE\\|ALL\\)/ERROR/") << QStringLiteral("/opt/project/app.log.config"));

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @raven-worx

                /* whats with the "1" here? */

                sed '1 s/foo/bar/': does the substitution only on line #1 :)

                @Kate_Software
                It's difficult to know what you actually want that line to be. @raven-worx may be right, and you don't want any backslashes, or maybe you genuinely want to search for e.g OFF\. Why don't you start by telling us whatever the actual command is when you type it into bash or whatever, and we'll take it from there?

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved