How to Replace String in file from Command Line for Windows/Mac/Linux

Very Fast cross-platform tool to Find and Replace text in file for Windows/Linux/Mac - freeware sd

Free Download (Windows, Mac and Linux)

After downloading, unzip it and put the binary file sd (sd.exe on Windows) in a folder which in the PATH environment variable

I wrote this article on Windows, and by setting up a Linux environment on Windows, then I can use many command line tools from Linux on Windows

I used command line tools find and sed to find the files and modify the strings in files, but now I don't need them any more

Before continuing, you can download this example replace.txt, place it in the current directory, and open it in a text editor. The editor should have the ability to automatically reload the changed file, and you can press Ctrl + Z to restore the text after replacement

Replace.txt:

Easiest is easiest
<li class="cba"><a href="z.html">~-remove - me-~</a></li> <li class="cba"><a href="y.html">keep me</a></li>
<!--comments-->
    remove me
    please
<!--comments-->

CLI text replace tool basic usage

sd 0.6.5

USAGE:
    sd.exe [OPTIONS] <find> <replace_with> [files]...

OPTIONS:
    -f, --flags <flags>
            Regex flags. May be combined (like `-f mc`).

            c - case-sensitive
            i - case-insensitive
            m - multi-line matching
            w - match full words only
    -h, --help
            Prints help information

    -i, --in-place
        (Deprecated). Modify the files in-place. Otherwise, transformations will be emitted to     STDOUT by default
    -s, --string-mode
            Treat expressions as non-regex strings
    -p, --preview


    -V, --version
            Prints version information


ARGS:
    <find>
            The regexp or string (if -s) to search for
    <replace_with>
        What to replace each match with. Unless in string mode, you may use captured values like     $1, $2, etc
    <files>...
            The path to file(s). This is optional - sd can also read from STDIN

How to find replace text with raw string literal (No Regex)

Find the text Easiest in the file replace.txt, and replace all instances with Easy, case sensitive

$ sd --string-mode Easiest Easy replace.txt

How to find and replace raw literal string in file

How to replace string ignore case (case insensitive)

Replace Easiest with easy, case insensitive

$ sd --string-mode --flags i Easiest easy replace.txt

Case insensitive Search Replace Text in File

How to search and replace string using Regular Expression

Read the Regex Syntax Guide first

Here is a Regex example that removes some text from the second line from replace.txt

$ sd '(?:<[^>]+>){2}~-[\w\s-]+-~(?:<[^>]+>){2}' '' replace.txt

Use Regular Expression Replace string in file

How to search and replace multiple lines string

Here is a Regular Expression example that replaces multiple comments with empty string:

$ sd '(?s)<!--comments-->.+?<!--comments-->' '' replace.txt

How to replace multiple lines text in file

To understand this example, you need to understand Regular Expression flags:

Flags are each a single character. For example, (?x) sets the flag x and (?-x) clears the flag x. > Multiple flags can be set or cleared at the same time: (?xy) sets both the x and y flags and (?x-y)> sets the x flag and clears the y flag All flags are by default disabled unless stated otherwise. They are:

  • i case-insensitive: letters match both upper and lower case
  • m multi-line mode: ^ and $ match begin/end of line
  • s allow . to match \n

Here we use the flag (?s) to allowing . to match new line(\n)

. can match any character except new line (includes new line with s flag)

How to modify string in multiple files recursively

Please read the article How to Find File with Name, Extension, Size and Date in directory on Windows/Mac first

We use sd with fd to replace strings in many files

Find all html files, and replace raw string www.easiestsoft.com to easiestsoft.com, case insensitive:

$ sd --string-mode --flags i `www.easiestsoft.com` `easiestsoft.com` ($fd --ignore-case --type file --extension html)

How to replace text in thousands files quickly

When using the above command to replace string in thousands of files, you may encounter argument list too long or too many files errors

This is the command line string length limit enforced by operation system, we can use xargs in combination to avoid this limitation

But Windows system does not have xargs, what should we do?

You can first follow the guide to set up a minimal Linux system on Windows

Here is the command to replace text in thousands of files:

$ fd -0 --ignore-case --type file --extension html | xargs -0 sd --string-mode --flags i `www.easiestsoft.com` `easiestsoft.com`

If there is a risk of filenames containing spaces or other special characters, you should remember to use the -0 flag

2019-12-20 King Eca