Add double quotes into string

Mar 1, 2013 at 11:08am
Hi,
Can anyone point me to add double quotes into string.I want to prepare command into following way with string.

awk 'BEGIN {printf "x\n%10s\t%10s\n", "Vol", "Current"}{if($1+0==$1){printf "%10.5f\t%10.5f\n", $2, $3}} END {printf "y\n"}'


Mar 1, 2013 at 11:36am
That's unreadable.

Put the program in a file and call it with:
awk -f prog.awk
Last edited on Mar 1, 2013 at 11:37am
Mar 1, 2013 at 12:07pm
"awk 'BEGIN {printf \"x\\n%10s\\t%10s\\n\", "Vol\", \"Current\"}{if($1+0==$1){printf \"%10.5f\\t%10.5f\\n\", $2, $3}} END {printf \"y\\n\"}'"


That is each ( " ) shall be substituted for ( \" ) and each ( \ ) for ( \\ ).
Last edited on Mar 1, 2013 at 12:08pm
Mar 1, 2013 at 12:21pm
That's even more unreadable. I take one day someone will need to support this, right?
Mar 1, 2013 at 12:48pm
Or you can use a raw string literal introduced in C++ 2011. In this case there is no need to double \ and to use \" instead of ".
I advice to use a raw string literal.

For example

1
2
3
4
5
6
7
8
9
10
#include <iostream> 
#include <string>     

int main()
{
     std::string s( R"(awk 'BEGIN {printf "x\n%10s\t%10s\n", "Vol", "Current"}{if($1+0==$1){printf "%10.5f\t%10.5f\n", $2, $3}} END {printf "y\n"}')" );
     std::cout << s << std::endl;

        return 0;     
}
Last edited on Mar 1, 2013 at 12:59pm
Mar 1, 2013 at 12:52pm
I advice to use a raw string literal.
In AWK?
Mar 1, 2013 at 12:58pm
@kbw
I advice to use a raw string literal.
In AWK?


In the C++ program.
Mar 1, 2013 at 1:21pm
Hi,
I have tried to Raw string but it did not accept.Do I need any other header file. I added in string file. or as we have legacy header file. do I need update string header file

gcc version:
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-9)


1
2
3
4
5
6
7
8
9
put.cpp:6: error: `R' was not declared in this scope
put.cpp:6: error: stray '\' in program
put.cpp:6:52: invalid suffix "s" on integer constant
put.cpp:6: error: stray '\' in program
put.cpp:6:58: invalid suffix "s" on integer constant
put.cpp:6: error: stray '\' in program
put.cpp:6: error: stray '\' in program
put.cpp:6: error: stray '\' in program
put.cpp:6: error: stray '\' in program 
Last edited on Mar 1, 2013 at 1:23pm
Mar 1, 2013 at 1:29pm
No header is required to use raw string literals. It seems that you are using an old compiler that does not support raw string literals.
To be sure that string literals are useful you can check my code on-line at www.ideone.com selecting C++11 ( gcc 4.7.2).
If you have no a possibility to use a newer compiler then you should do as I pointed out in my first post.
Last edited on Mar 1, 2013 at 1:32pm
Mar 2, 2013 at 7:58am
Thx a lot for reply . it works for me.
Topic archived. No new replies allowed.