I want to grep a log file - which can be wery huge, often >= 600mb - and then tail the output to get exactly n lines.
To be more precise I expect to obtain the same output as if I type this command on the Windows/Linux prompt:
grep.exe -i "STRING" "PATH_TO_FILE" | tail.exe -n LINES_TO_TAIL
I'm a C# and .Net (Core) newbie so be kind with me. The following code is my attempt of getting something useful:
executableName = @"PATH_TO\grep.exe";
executableArgs = String.Format(@"-i {0} {1} | PATH_TO\\tail.exe -n {2}", paramList["Text"], moduleInfo.GetValue("LogPath"), paramList["MaxLines"]);
var processStartInfo = new ProcessStartInfo
{
FileName = executableName,
Arguments = executableArgs,
RedirectStandardOutput = true
};
Process process = Process.Start(processStartInfo);
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
I'm pretty sure that the problem could be the fact that using |
in the raw argument list is't the correct way to handle the pipe between two processes.
If I set UseShellExecute
to true
I get an exception with a message like "UseShellExecute" must always be false
.
Is there a reasonable and efficient way, even without using external executables, to obtain what I expect?