D: How do I pipe (|) through three programs using std.process?

kdevel kdevel at vogtner.de
Sat Nov 11 22:11:59 UTC 2023


On Saturday, 11 November 2023 at 17:29:14 UTC, BoQsc wrote:
> https://dlang.org/library/std/process.html
>
> How do I pipe (|) through three programs using std.process?
>
> ```
> echo This is a sample text | find "sample" | find "text"
> ```

```d
import std.stdio;
import std.process;

version (Windows) { enum Find = "find"; }
version (Posix) { enum Find = "grep"; }

int main (string [] args)
{
    auto p1 = pipe;
    auto p2 = pipe;

    auto pid1 = spawnProcess (args [1..$], stdin, p1.writeEnd);
    auto pid2 = spawnProcess ([Find, "sample"], p1.readEnd, 
p2.writeEnd);
    auto pid3 = spawnProcess ([Find, "text"], p2.readEnd, stdout);
    wait (pid1);
    wait (pid2);
    wait (pid3);
    return 0;
}
```

```
$ ./pip echo This is a sample text
This is a sample text
$ ./pip echo This is an ample text
```


More information about the Digitalmars-d-learn mailing list