0

I have a string as follows, although it throws an error when it gets to the colon, I have used @ to escape everything:

string vmListCommand = @"vim-cmd vmsvc/getallvms | sed '1d' | awk '{if ($1 > 0) print $1":"$2}'";
user1559618
  • 399
  • 3
  • 5
  • 16

4 Answers4

2

Remove @ and escape double quotes using \:

string vmListCommand = "vim-cmd vmsvc/getallvms | sed '1d' | awk '{if ($1 > 0) print $1\":\"$2}'";

You wrote:

I have used @ to escape everything

@ is used to change escaping bahaviour, not to escape everything. If a string is prefixed with @ then escape sequences (\) are ignored.

Zbigniew
  • 26,284
  • 6
  • 55
  • 64
  • If you want to read more about `@string` then take a look at [@(at) sign in file path/string](http://stackoverflow.com/questions/5179389/at-sign-in-file-path-string) and [What's the @ in front of a string in C#?](http://stackoverflow.com/questions/556133/whats-the-in-front-of-a-string-in-c) – Zbigniew Jul 28 '12 at 13:17
1

Use \ for escape in your string.

Example:

string str1 ="hello\\";
Embedd_0913
  • 15,581
  • 34
  • 96
  • 135
1

You need to remove the literal and escape

string vmListCommand = "vim-cmd vmsvc/getallvms | sed '1d' | awk '{if ($1 > 0) print $1\":\"$2}'";
paparazzo
  • 43,659
  • 20
  • 99
  • 164
1

There is one character that needs to be escaped in literal strings. The double quote ". If you don't escape it, how would the compiler know which " are part of the string, and which terminate the string?

To escape a " in a literal string, simply double it:

 @"vim-cmd vmsvc/getallvms | sed '1d' | awk '{if ($1 > 0) print $1"":""$2}'"

Alternatively you could switch to the normal string syntax, and escape with \.

CodesInChaos
  • 103,479
  • 23
  • 206
  • 257