当作为System.Diagnostics.Process运行时,sed命令提供不正确的输出

时间:2017-03-03 17:40:42

标签: c# .net sed process

给定一个包含以下行的文本文件helloworld.txt:

  

您好/世界

以下sed命令将输出删除了行的文件内容:

sed '\:^hello/world:d' helloworld.txt

但是,如果该命令作为System.Diagnostics.Process运行:

var process = new Process
{
    StartInfo =
    {
        FileName = "sed",
        Arguments = "'\\:^hello/world:d' helloworld.txt",
        RedirectStandardOutput = true,
        UseShellExecute = false
    }
};
process.Start();
while (!process.StandardOutput.EndOfStream)
{
    Console.WriteLine(process.StandardOutput.ReadLine());
}
process.WaitForExit();

然后输出为“hello / world”(即不删除匹配行)。

为什么会这样?

1 个答案:

答案 0 :(得分:0)

我能够通过添加额外的反斜杠来实现这​​一点:

"'\\\:^hello/world:d' helloworld.txt"

根据this answer中的信息。