SD

https://img.shields.io/badge/wrapper_version-v9.13.0-10785b https://img.shields.io/github/issues-pr/snakemake/snakemake-wrappers/utils/sd?label=version%20update%20pull%20requests&color=1cb481

sd is an intuitive find & replace CLI. Faster than gnu sed for plain text replacement.

URL: https://github.com/chmln/sd

Example

This wrapper can be used in the following way:

rule test_sd:
    input:
        "file.txt",
    output:
        "replaced.txt",
    log:
        "test_sd.log",
    threads: 1
    params:
        # Pattern to search in original text
        before="change /this/path",
        # Replacement pattern in the output text
        after="keep /that/one",
        # Any non-positional argument
        extra="",
    wrapper:
        "v9.13.0/utils/sd"


rule test_sd_regex:
    input:
        "file.txt",
    output:
        "trailing_whitespace_trimmed.txt",
    log:
        "test_sd_regex.log",
    threads: 1
    params:
        before=r"\s+$",
        after=r"",
        extra="",
    wrapper:
        "v9.13.0/utils/sd"

Note that input, output and log file paths can be chosen freely.

When running with

snakemake --use-conda

the software dependencies will be automatically deployed into an isolated environment before execution.

Notes

See patterns examples at https://github.com/chmln/sd#quick-guide

Software dependencies

  • sd=1.0.0

Input/Output

Input:

  • Path to file to search and replace patterns into

Output:

  • Path to processed file

Params

  • before: Regular expression to search

  • after: Replacement expression

  • extra: Optional parameters for sd, any non-positional parameter

Authors

  • Thibault Dayris

Code

# coding: utf-8

"""Snakemake wrappers for sd"""

__author__ = "Thibault Dayris"
__copyright__ = "Copyright 2025, Thibault Dayris"
__license__ = "MIT"

from snakemake.shell import shell
from shlex import quote

extra = snakemake.params.get("extra", "")
log = snakemake.log_fmt_shell(stdout=False, stderr=True)

# Snakemake does not treat empty strings as quotable arguments
# We use shlex.quote instead of `:q` syntax to force quotes around empty strings
before = quote(snakemake.params.get("before", ""))
after = quote(snakemake.params.get("after", ""))

# Since `sd` changes files in place,
# we use `cat` to disable this behavior
shell(
    "cat {snakemake.input:q} | "
    "sd {extra} {before} {after} "
    "> {snakemake.output:q} {log}"
)