Skip to main content

Crate sed_rs

Crate sed_rs 

Source
Expand description

§sed-rs

A GNU-compatible sed (stream editor) implementation in Rust.

This crate can be used both as a standalone command-line tool and as a library for programmatic stream editing.

§Quick start

// Simple substitution
let output = sed_rs::eval("s/hello/world/", "hello there\n").unwrap();
assert_eq!(output, "world there\n");

// Global substitution
let output = sed_rs::eval("s/o/0/g", "foo boo\n").unwrap();
assert_eq!(output, "f00 b00\n");

// Delete lines matching a pattern
let output = sed_rs::eval("/^#/d", "# comment\ncode\n").unwrap();
assert_eq!(output, "code\n");

// Multiple commands
let output = sed_rs::eval("s/a/X/; s/b/Y/", "ab\n").unwrap();
assert_eq!(output, "XY\n");

§Advanced usage

For more control, use Sed directly:

use sed_rs::Sed;

let mut sed = Sed::new("s/foo/bar/g").unwrap();
sed.quiet(true);           // suppress auto-print (-n)

let output = sed.eval("no match here\n").unwrap();
assert_eq!(output, "");    // quiet mode: nothing printed unless explicit `p`

§Using the lower-level API

The command and engine modules expose the parser and execution engine for full control:

use sed_rs::{command, engine, Options};

let commands = command::parse("2d").unwrap();
let options = Options::default();
let engine = engine::Engine::new(commands, &options).unwrap();
// engine.run(&[]) reads from stdin, engine.run(&[path]) reads files

Re-exports§

pub use cli::Options;
pub use error::Error;
pub use error::Result;

Modules§

cli
command
engine
error
unescape

Structs§

Sed
A configured sed instance that can process text.

Functions§

eval
Evaluate a sed script against an input string and return the result.