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
  • 449
  • 3
  • 6
  • 17

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
  • 27,184
  • 6
  • 59
  • 66
  • 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
  • 16,125
  • 37
  • 97
  • 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
  • 44,497
  • 23
  • 105
  • 176
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
  • 106,488
  • 23
  • 218
  • 262