Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Working with Input

Sending data to process stdin is fundamental to building effective data processing pipelines. proc provides several approaches depending on your data source and use case.

Choosing the Right Input Method

The most common approach is using .run() to pipe output from one process directly to another, creating efficient process-to-process pipelines:

await run("cat", "file.txt").run("grep", "pattern").toStdout();

When you have in-memory data that you want to send to a process, enumerate() wraps your data and makes it pipeable:

await enumerate(["line1", "line2"]).run("grep", "1").toStdout();

For file input, read() creates a stream directly from the file system:

await read("input.txt").run("grep", "pattern").toStdout();

When you need generated sequences, range() creates numeric streams that you can transform and pipe:

await range({ to: 100 }).map((n) => n.toString()).run("shuf").toStdout();

Piping Between Processes

The most common way to provide input is piping output from one process directly to another. This creates efficient data flows without intermediate storage:

import { run } from "jsr:@j50n/proc@0.24.6";

await run("echo", "hello")
  .run("tr", "a-z", "A-Z") // Receives "hello" as stdin
  .toStdout();
// HELLO

Working with In-Memory Data

When you have data in memory that you want to send to a process, enumerate() makes any iterable pipeable to processes:

import { enumerate } from "jsr:@j50n/proc@0.24.6";

const data = ["line 1", "line 2", "line 3"];

await enumerate(data)
  .run("grep", "2")
  .toStdout();
// line 2

Reading from Files

For file input, read() creates a stream directly from the file system, allowing you to process files of any size efficiently:

import { read } from "jsr:@j50n/proc@0.24.6";

await read("input.txt")
  .run("grep", "pattern")
  .toStdout();

Real-World Examples

Filter Data

await read("data.txt")
  .run("grep", "ERROR")
  .run("sort")
  .run("uniq")
  .toStdout();

Transform and Process

await read("input.txt")
  .lines
  .map((line) => line.toUpperCase())
  .run("sort")
  .toStdout();

Generate and Process

import { range } from "jsr:@j50n/proc@0.24.6";

await range({ to: 100 })
  .map((n) => n.toString())
  .run("shuf") // Shuffle
  .run("head", "-10")
  .toStdout();

Next Steps