GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Preface
  • 1. Rust Regex
  • 2. hck
  • 3. sd
  • 4. ripgrep
  • 4.1. Options
  • 4.2. PCRE2
  • 4.3. Multiple Files
  • 5. frawk
  • 6. huniq
  • 7. xsv
  • 8. zet
  • 9. htmlq
  • Source code

This view

  • Original page
  • New address

GeezerVison is showing learnbyexample.github.io — sd - Command line text processing with Rust tools



warning warning warning This is a work-in-progress draft version.



sd

From github: sd:

sd is an intuitive find & replace CLI.

Installation

See sd: installation for details.

Search and replace

stdin example:

# note that all the occurrences are replaced
# first argument is the search text
# second argument is the replacement text
$ printf '1,2,3,4\na,b,c,d\n' | sd ',' '-'
1-2-3-4
a-b-c-d

$ echo 'I like "mango" and "guava"' | sd '"[^"]+"' 'X'
I like X and X

File example. By default, sd modifies the input in-place.

$ cat colors.txt
deep blue
light orange
blue delight

$ sd 'blue' 'green' colors.txt
$ cat colors.txt 
deep green
light orange
green delight

Use -p option to preview the results instead of in-place modification.

$ sd -p 'green' 'red' colors.txt
deep red
light orange
red delight

Fixed string substitution

Use -s option to treat the search and replace arguments as fixed strings.

# same as: sd 'in\+put' '$$a'
$ echo 'sample in+put' | sd -s 'in+put' '$a'
sample $a

Here's a multiline example:

$ cat ip.txt
This is a multiline
sample input with lots
of special characters
like . () * [] $ {}
^ + ? \ and ' and so on.
This post shows how
you can do fixed
-string multiline
search with cli tools.

$ sd -ps 'like . () * [] $ {}
^ + ? \ and' '====
----
====' ip.txt
This is a multiline
sample input with lots
of special characters
====
----
==== ' and so on.
This post shows how
you can do fixed
-string multiline
search with cli tools.




More to come

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.