xed

xed is a command-line tool for performing basic text transformations. Modeled after the utility sed, xed provides an intuitive interface and supports multi-line regular expressions by default.

Example usage:

$ echo 'Hello, world!' | xed replace Hello Goodbye
Goodbye, world!

Read on to learn more.

Table of Contents

Installation

xed can run on any system that has Python 3.6 or higher installed. To install Python, refer to Python's Getting Started guide.

To install xed, run:

$ pip install xed

Overview

Getting Help

Once installed, you can use the --help flag to learn more about xed. For example, to see the set of commands offered, run:

$ xed --help

You can also get more information about specific commands. The following example shows how to get more details about the replace command:

$ xed replace --help

Inputs

All xed commands that manipulate text read data from standard input, unless one or more file paths are provided as positional arguments. Consider this standard input example:

$ echo 'Hello, world!' | xed replace Hello Goodbye
Goodbye, world!

And this file example:

$ echo 'Hello, world!' > my-file.txt
$ xed replace Hello Goodbye my-file.txt
Goodbye, world!

Outputs

By default, the results of the commands that manipulate text are written to standard output. If the input is one or more files, you can use the --in-place flag or its -i shorthand to overwrite the files in-place:

$ echo "Hello, world!" > my-file.txt
$ xed replace Hello Goodbye my-file.txt --in-place
$ cat my-file.txt
Goodbye, world!

Commands

replace

The replace command takes a regular expression and replaces each matching instance with a replacement text. For example, the following invocation replaces all numbers with the letter N:

$ echo 1bbb123ccc123456ddd | xed replace [0-9]+ a
NbbbNcccNddd

xed uses the regular expression engine that powers Python's re module, with the MULTILINE and DOTALL options enabled.

Since xed uses the DOTALL option, regular expressions can match text over multiple lines:

$ cat <<EOF | xed replace '\nREPLACE\nME' ', world!'
Hello
REPLACE
ME
EOF
Hello, world!

You can also use capture groups:

$ echo repeat-me but-not-me | xed replace '(repeat-me)' '\1 \1'
repeat-me repeat-me but-not-me

For more guidance on regular expression features supported, refer to the Python re documentation.

delete

The delete command is syntactic sugar for replace with the empty string as the replacement text. Example usage:

$ echo delete-me but-not-me | xed delete 'delete-me '
but-not-me

Similar to replace, you can delete multiple lines:

$ cat <<EOF | xed delete '\nDELETE\nME'
Hello, world!
DELETE
ME
EOF
Hello, world!

Sometimes you need to modify only files that match a regular expression that is different than the regular expression given to the replace or delete command. grep can be used for this kind of filtering, but it's awkward to use because, by default, grep's output includes more than the file names and grep uses a regular expression syntax not compatible with Python's re module.

search simplifies this use-case. Consider this example:

$ echo 'Hello, world!' > my-file.txt
$ echo 'Goodbye, world!' > my-other-file.txt
$ xed replace world Alice $(xed search Hello *)
Hello, Alice!
Goodbye, world!

More Information

xed is authored by Aryan Naraghi. The source code is available on GitHub. xed is distributed using PyPI.