sd

Intuitive find & replace CLI (sed alternative)
授权协议 MIT License
开发语言 JavaScript
所属分类 程序开发、 正则表达式工具
软件类型 开源软件
地区 不详
投 递 者 师承弼
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

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
  • SD卡初始化 1 寄存器介绍 SD卡内部有7个寄存器,分别为:OCR、CID、CSD、SCR、RCA、CSR和SSR寄存器(mmc卡没有SCR、SSR寄存器)。 OCR寄存器:OCR寄存器:32bit。这个寄存器存储了卡的VDD电压轮廓图。任何标准的SD卡控制器可以使用2V-3.6V的工作电压来让SD卡能执行这个电压的识别操作。OCR寄存器存储了在访问卡数据是所需的电压范围。OCR的第30位反映该

  • 一、SD卡介绍 传送门:https://blog.csdn.net/qq_39507748/article/details/113195336 初始化以及读写操作的流程文章中也有说到。 二、实验内容 有一个开关switch,可以切换读取的图片,也即切换读取的扇区地址。每张图片在SD卡中的起始扇区地址以及地址范围(或者说图片大小)可以使用winhex软件进行查看。 读取SD卡16bit的图片数据,然

  • 上文已经介绍了关于SD的初始化流程,在完成初始化后就可以进行扇区读写了 注意事项: SD卡只能进行整扇区读写,即512byte,每次读写必须为512byte整数倍 SD卡分2种寻址方式 1:字节寻址 2:扇区寻址 CMD读写命令说明:         0~7:cmd命令序号;                                           8~39:扇区地址(注意寻址方式)  

相关阅读

相关文章

相关问答

相关文档