Skip to main content
# fxr

[![Travis](https://travis-ci.org/pmav99/fxr.svg?branch=master)](https://travis-ci.org/pmav99/fxr)
[![coveralls](https://coveralls.io/repos/pmav99/fxr/badge.svg?branch=master&service=github)](https://coveralls.io/r/pmav99/fxr)

`fxr` (pronounced "fixer") is a CLI utility that can be used to find matching patterns in text files
and:

* append/prepend lines
* delete lines before/after the matching line.
* replace text

In other words, you can consider it as a replacement for `ag`, `xargs` and `sed` (with a nicer API!)

**Warning**: *`fxr` currently has no dry-run mode and by default changes files inplace. If you want
backups, you need to explicitly enable them!

## Installation

All you need is Python `2.7+/3.3+` and a search program like
[`ag`](https://github.com/ggreer/the_silver_searcher):

```
pip install -U fxr
pip install --user -U fxr # this will install fxr on ~/.local/
```

The latest version is `0.2.5`:

## Modes of operation

`fxr` has three modes of operations:

* `fxr add`
* `fxr delete`
* `fxr replace`

### `fxr add`

In this mode you search for lines matching `<pattern>` and you append/prepend text to them.

```
fxr add <pattern> <added_text> # Appends text to lines matching pattern
fxr add --prepend <pattern> <added_text> # Prepends text to lines matching pattern
```

### `fxr delete`

In this mode you search for lines matching `<pattern>` and you can:

1. delete N lines preceding the matching line
2. delete M lines following the matching line
3. delete the matching line itself
4. or any combination of the above!

E.g. to delete 3 lines before the line matching pattern, 2 lines after it and the matching line
itself:

```
fxr delete --before 3 --after 2 --include_line <pattern>
```

### `fxr replace`

In this mode you can replace text on a single line. This is more or less equivalent to:

```
ag <pattern> -l | xargs sed -i 's/<pattern>/<replacement>/g'
```

You can use it like this:

```
fxr replace <pattern> <replacement>
```

## Common interface

All the above subcommands share the following flags/arguments:

* `--backup`: The provided value is used as a prefix for the backup files. If it is not provided,
the changes are being done in-place.
* `--literal`: When you set this flag, the pattern will not be parsed as a regex.
* `--raise-if-no-change`: When you set this flag, an exception will be raised if there were no
changes in the file.
* `--single <filename>`: When you specify this argument, fxr will only try to do its magic on the
specified file. I.e. `ag` will not be used.
* `--search_prog`: If you don't like `ag` you can specify an alternate program (e.g. `rg`).
* `--search_args`: You can specify additional arguments for the search program.

## Notes

### Rationale

There were two compelling arguments that made me write this:

1. `sed` regex engine is quite limited. E.g. there is no support for look-aheads and other more
advanced features, which you don't really need, until you need them!
([link](https://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html)).

2. The API for combining `ag`, `sed` and `xargs` is clunky at best (e.g. repeating `<pattern>` both
in `ag` and `sed` etc). E.g.:
`ag <pattern> -l | xargs sed -i 's/<pattern>/<replacement>/g'`


### Performance

The script has not been written with performance in mind. Since the search for matching files is
being done using `ag`, performance shouldn't be too bad, but keep in mind that the main use case
is to make changes to source code and configuration files; not a multi-GB CSV file/database dump.

### Search programs

If you wish to use a different program than `ag` you can do so by using the appropriate `CLI` argument.



Release files for fxr 0.2.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for fxr 0.2.5
File Interpreter ABI Platform
fxr-0.2.5-py2.py3-none-any.whl Python 2, Python 3 none any Details

Release files / fxr-0.2.5-py2.py3-none-any.whl

Download URL fxr-0.2.5-py2.py3-none-any.whl
Size 8.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
0f16bf103fd83d433c583c971e8d2ae70ddd3bda5c9a5c39a5e7c12965b64d4b
BLAKE2b-256 checksum
How to use checksums
d334619225a557219a88f92ddf190599df94185a1f1677e3da56f0242ac7361d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.5 This release

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page