Reputation: 1407
I have the awk command wch I want to wrap in system().
std::string cmd = "awk -F\| '{ var1=$2; var2=$5; print var1, var2 }' 1.txt";
system(cmd.c_str());
Getting the error as,
error: unknown escape sequence '\|'
I have tried giving the quotes in different way. But, nothing helps.
Upvotes: 0
Views: 1386
Reputation: 74635
You just need to escape the \
:
std::string cmd = "awk -F\\| '{ var1=$2; var2=$5; print var1, var2 }' 1.txt";
//-----------------------^
When you create your C++ string, \
is treated as an escape character, unless you escape it.
Alternatively in this case, you could just quote the delimiter, avoiding the need to escape it:
std::string cmd = "awk -F'|' '{ print $2, $5 }' 1.txt";
I assume that your script may be more complex that this in reality but I simplified it for you anyway, in case that helps.
A final option, using raw string literals (which are supported in C++11):
std::string cmd = R"awk -F\| '{ print $2, $5 }' 1.txt";
The R
prefix means that the \
is no longer interpreted as an escape character.
Upvotes: 2
Reputation: 2768
maybe scaping the -F"|" quotes??
std::string cmd = "awk -F\"|\" '{ var1=$2; var2=$5; print var1, var2 }' 1.txt";
system(cmd.c_str());
Upvotes: 0