简体   繁体   English

使用动态值从模板txt文件输出文本

[英]Output text from template txt file with dynamic values

I have a template txt file (example): 我有一个模板txt文件(示例):

test_asd = value test_asd =值
this is the end of the file 这是文件的结尾

From which I need to create other text files with different values in the place of value 我需要从中创建其他具有不同值的文本文件来代替value

I'm writing a program to use this in C# (using a child process cmd.exe and commands) 我正在编写一个程序在C#使用此程序(使用子进程cmd.exe和命令)

I have tried setting an environment variable but the output is not different from the original template file. 我尝试设置环境变量,但是输出与原始模板文件没有区别。

test.txt: test.txt:

test_asd = %VALUE% test_asd =%VALUE%
this is the end of the file 这是文件的结尾

C#: C#:

Environment.SetEnvironmentVariable("VALUE", "test");

Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "cmd.exe";
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
p.StartInfo = psi;

p.Start();

p.StandardInput.WriteLine("type test.txt");
p.StandardInput.WriteLine("exit");

p.WaitForExit();

string output = p.StandardOutput.ReadToEnd();
Console.WriteLine(output);

Console.ReadLine(); // wait for 'Enter' to exit

Is there a way to do this? 有没有办法做到这一点?

I figured it out, here is the command: 我想通了,这是命令:

type test.txt | sed.exe -e "s/\${value}/test/"

using UnxUtils 使用UnxUtils

thanks to this SO question 由于这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM