Escape character '\' on an string that is passed to QProcess.start()
-
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. -
@Kate_Software
how exactly did you use it? -
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.
-
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")); -
@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")); -
@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
-
/* 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.gOFF\
. 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?