All Projects → chmln → Sd

chmln / Sd

Licence: mit
Intuitive find & replace CLI (sed alternative)

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to Sd

Fd
A simple, fast and user-friendly alternative to 'find'
Stars: ✭ 19,851 (+620.54%)
Mutual labels:  terminal, command-line, regex
Command Line Text Processing
⚡ From finding text to search and replace, from sorting to beautifying text and more 🎨
Stars: ✭ 9,771 (+254.66%)
Mutual labels:  command-line, regex, text-processing
Fsq
A tool for querying the file system with a SQL-like language.
Stars: ✭ 60 (-97.82%)
Mutual labels:  terminal, command-line, regex
Xioc
Extract indicators of compromise from text, including "escaped" ones.
Stars: ✭ 148 (-94.63%)
Mutual labels:  command-line, regex, text-processing
Typin
Declarative framework for interactive CLI applications
Stars: ✭ 126 (-95.43%)
Mutual labels:  terminal, command-line
Nnn
n³ The unorthodox terminal file manager
Stars: ✭ 13,138 (+376.88%)
Mutual labels:  terminal, command-line
Desktoppr
Simple command line tool to set the desktop picture on macOS
Stars: ✭ 127 (-95.39%)
Mutual labels:  terminal, command-line
Progressbar
A really basic thread-safe progress bar for Golang applications
Stars: ✭ 2,212 (-19.71%)
Mutual labels:  terminal, command-line
Sty
String styling for your terminal.
Stars: ✭ 129 (-95.32%)
Mutual labels:  terminal, command-line
Terminalizer
🦄 Record your terminal and generate animated gif images or share a web player
Stars: ✭ 12,165 (+341.56%)
Mutual labels:  terminal, command-line
Ls Go
A more colorful, user-friendly implementation of `ls` written in Go
Stars: ✭ 162 (-94.12%)
Mutual labels:  terminal, command-line
Warp
Secure and simple terminal sharing
Stars: ✭ 1,548 (-43.81%)
Mutual labels:  terminal, command-line
Goto
Navigate long command lines using a minimalistic char-based decision tree.
Stars: ✭ 112 (-95.93%)
Mutual labels:  terminal, command-line
Mandown
man-page inspired Markdown viewer
Stars: ✭ 173 (-93.72%)
Mutual labels:  terminal, command-line
Httpcat
httpcat is a simple utility for constructing raw HTTP requests on the command line.
Stars: ✭ 109 (-96.04%)
Mutual labels:  terminal, command-line
Asciigraph
Go package to make lightweight ASCII line graph ╭┈╯ in command line apps with no other dependencies.
Stars: ✭ 1,805 (-34.48%)
Mutual labels:  terminal, command-line
Smenu
smenu started as a lightweight and flexible terminal menu generator, but quickly evolved into a powerful and versatile CLI selection tool for interactive or scripting use.
Stars: ✭ 1,906 (-30.82%)
Mutual labels:  terminal, command-line
Command Line Api
Command line parsing, invocation, and rendering of terminal output.
Stars: ✭ 2,418 (-12.23%)
Mutual labels:  terminal, command-line
Sbt Prompt
An SBT plugin for making your SBT prompt more awesome
Stars: ✭ 107 (-96.12%)
Mutual labels:  terminal, command-line
Word Wrap
Wrap words to a specified length.
Stars: ✭ 107 (-96.12%)
Mutual labels:  terminal, command-line

sd - s[earch] & d[isplace]

sd is an intuitive find & replace CLI.

The Pitch

Why use it over any existing tools?

Painless regular expressions

sd uses regex syntax that you already know from JavaScript and Python. Forget about dealing with quirks of sed or awk - get productive immediately.

String-literal mode

Non-regex find & replace. No more backslashes or remembering which characters are special and need to be escaped.

Easy to read, easy to write

Find & replace expressions are split up, which makes them easy to read and write. No more messing with unclosed and escaped slashes.

Smart, common-sense defaults

Defaults follow common sense and are tailored for typical daily use.

Comparison to sed

While sed does a whole lot more, sd focuses on doing just one thing and doing it well.

Some cherry-picked examples, where sd shines:

  • Simpler syntax for replacing all occurrences:
    • sd: sd before after
    • sed: sed s/before/after/g
  • Replace newlines with commas:
    • sd: sd '\n' ','
    • sed: sed ':a;N;$!ba;s/\n/,/g'
  • Extracting stuff out of strings containing slashes:
    • sd: echo "sample with /path/" | sd '.*(/.*/)' '$1'
    • sed: use different delimiters every time depending on expression so that the command is not completely unreadable
      • echo "sample with /path/" | sed -E 's/.*(\\/.*\\/)/\1/g'
      • echo "sample with /path/" | sed -E 's|.*(/.*/)|\1|g'
  • In place modification of files:
    • sd: sd before after file.txt
    • sed: you need to remember to use -e or else some platforms will consider the next argument to be a backup suffix
      • sed -i -e 's/before/after/g' file.txt

Benchmarks

Simple replacement on ~1.5 gigabytes of JSON

hyperfine -w 3 'sed -E "s/\"/\'/g" *.json >/dev/null' 'sd "\"" "\'" *.json >/dev/null' --export-markdown out.md

Command Mean [s] Min…Max [s]
sed -E "s/\"/'/g" *.json >/dev/null 2.338 ± 0.008 2.332…2.358
sed "s/\"/'/g" *.json >/dev/null 2.365 ± 0.009 2.351…2.378
sd "\"" "'" *.json >/dev/null 0.997 ± 0.006 0.987…1.007

Result: ~2.35 times faster

Regex replacement on a ~55M json file:

hyperfine \
'sed -E "s:(\w+):\1\1:g" dump.json >/dev/null'\
"sed 's:\(\w\+\):\1\1:g' dump.json >/dev/null"\
'sd "(\w+)" "$1$1" dump.json >/dev/null'
Command Mean [s] Min…Max [s]
sed -E "s:(\w+):\1\1:g" dump.json >/dev/null 11.315 ± 0.215 11.102…11.725
sed 's:\(\w\+\):\1\1:g' dump.json >/dev/null 11.239 ± 0.208 11.057…11.762
sd "(\w+)" "$1$1" dump.json >/dev/null 0.942 ± 0.004 0.936…0.951

Result: ~11.93 times faster

Installation

Cargo

Cargo is the Rust package manager.

You can install cargo by

curl https://sh.rustup.rs -sSf | sh

Then

cargo install sd

Alpine Linux

apk add sd

Before installing, ensure the appropriate repository is enabled.

Arch Linux

pacman -S sd

Gentoo (unc3nsored overlay)

emerge -av sys-apps/sd

Before installing, ensure the appropriate overlay is enabled.

Fedora

dnf install sd

FreeBSD

pkg install sd

Windows

choco install sd-cli

macOS

brew install sd

Void Linux

xbps-install sd

Quick Guide

  1. String-literal mode. By default, expressions are treated as regex. Use -s or --string-mode to disable regex.
> echo 'lots((([]))) of special chars' | sd -s '((([])))' ''
lots of special chars
  1. Basic regex use - let's trim some trailing whitespace
> echo 'lorem ipsum 23   ' | sd '\s+$' ''
lorem ipsum 23
  1. Capture groups

Indexed capture groups:

> echo 'cargo +nightly watch' | sd '(\w+)\s+\+(\w+)\s+(\w+)' 'cmd: $1, channel: $2, subcmd: $3'
cmd: cargo, channel: nightly, subcmd: watch

Named capture groups:

> echo "123.45" | sd '(?P<dollars>\d+)\.(?P<cents>\d+)' '$dollars dollars and $cents cents'
123 dollars and 45 cents

In the unlikely case you stumble upon ambiguities, resolve them by using ${var} instead of $var. Here's an example:

> echo '123.45' | sd '(?P<dollars>\d+)\.(?P<cents>\d+)' '$dollars_dollars and $cents_cents'
 and 
 
> echo '123.45' | sd '(?P<dollars>\d+)\.(?P<cents>\d+)' '${dollars}_dollars and ${cents}_cents'
123_dollars and 45_cents
  1. Find & replace in a file
> sd 'window.fetch' 'fetch' http.js

That's it. The file is modified in-place.

To preview changes:

> sd -p 'window.fetch' 'fetch' http.js 
  1. Find & replace across project

This example uses fd.

Good ol' unix philosophy to the rescue.

sd 'from "react"' 'from "preact"' $(fd --type file)

Same, but with backups (consider version control).

for file in $(fd --type file); do
  cp "$file" "$file.bk"
  sd 'from "react"' 'from "preact"' "$file"; 
done

Edge cases

replace/-with string needs extra -- before it, if starts with double-minus (this is a limitation of the bash itself)

echo "test/test" | sd '/' -- '--inteneded--'
test--inteneded--test

echo "start/--/end" | sd --string-mode -- '--' 'middle'
start/middle/end
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].