An open API service indexing awesome lists of open source software.

https://github.com/gbbirkisson/regop


https://github.com/gbbirkisson/regop

Last synced: about 1 year ago
JSON representation

Awesome Lists containing this project

README

          




Logo


regop


Easy file manipulation with regex and operators

* [Usage 📖](#usage-)
* [TL;DR](#tldr)
* [Regex](#regex)
* [Operators](#operators)
* [Table](#table)
* [Installation 💻](#installation-)
* [Using cargo](#using-cargo)
* [Using install script](#using-install-script)
* [Download pre-built binaries](#download-pre-built-binaries)
* [Development 🚧](#development-)

## Usage 📖

### TL;DR

Use the `-r` to define regular expression capture groups and `-o` to define operators to
manipulate files:

```bash
# Increment edition in Cargo.toml by one
$ regop \
-r 'edition = "(?[^"]+)' \
-o ':inc' \
Cargo.toml

┌───────────────────────────────────────────────────────────────────────────────
│ Cargo.toml
├───────────────────────────────────────────────────────────────────────────────
│ 3 3 │ version = "0.2.3"
│ 4 │-edition = "2024"
│ 4 │+edition = "2025"
│ 5 5 │ repository = "https://github.com/gbbirkisson/regop"
└───────────────────────────────────────────────────────────────────────────────
```

```bash
# Swap anyhow major and patch version, increment minor by 3
$ regop \
-r 'anyhow = "(?\d+)\.(?\d+)\.(?\d+)"' \
-o ':swap:' \
-o ':inc:3' \
Cargo.toml
┌───────────────────────────────────────────────────────────────────────────────
│ Cargo.toml
├───────────────────────────────────────────────────────────────────────────────
│ 20 20 │ [dependencies]
│ 21 │-anyhow = "1.0.95"
│ 21 │+anyhow = "95.3.1"
│ 22 22 │ atty = "0.2.14"
└───────────────────────────────────────────────────────────────────────────────
```

```bash
# Multiply version numbers and convert package names to uppercase
$ regop \
-r '(?\w+) = "(?\d+)"' \
-o ':upper' \
-o ':mul:2' \
dependencies.txt

# Append suffix to filenames and prepend prefix
$ regop \
-r 'file: (?\w+)' \
-o ':prepend:backup_' \
-o ':append:.bak' \
config.txt
```

```bash
# Update all major versions in all toml files
$ find -name '*.toml' | regop \
-w \
-r '"(?\d+)\.(?\d+)\.(?\d+)"' \
-o ':inc'
```

```bash
# Read from stdin and write to stdout
$ cat Cargo.toml | regop -w \
-r "version = \"(?\d)\.(?\d)" \
-o ":rep:21" \
-
```

### Regex

The first piece of the puzzle is that you use regular expressions with named capture groups to
extract some values in files. We use the rust
[regex](https://docs.rs/regex/latest/regex/#example-named-capture-groups) crate, so you can use
that documentation for reference.

Another excellent resource is [regex101](https://regex101.com/). The site fully supports the
rust [regex](https://docs.rs/regex/latest/regex/#example-named-capture-groups) crate and can
help you make sense of complicated expressions:

Here are some examples from [regex101](https://regex101.com/):
- Extract `major`, `minor` and `patch` version from file: [link](https://regex101.com/r/wR5BJ5/1)
- Extract `H2` subheadings from markdown: [link](https://regex101.com/r/ixUPEW/1)

### Operators

The second piece is that you can manipulate your capture groups with operators. Operators
take the form of:

```
:operation:parameter
```

Where:

- `` is the name of your capture group (include the `<` `>` signs).
- `operation` is the name of the desired operation (see [table](#table) below).
- `parameter` is the parameter to the operation (see [table](#table) below). Note that
`parameter` can reference another named capture.

#### Table

| Name | Description | Default | Valid parameters | Examples |
| --------- | --------------------- | ------- | --------------------- | ----------------------------------------------- |
| `inc` | Increment number | `1` | `int`, `` | `:inc`, `:inc:5`, `:inc:` |
| `dec` | Decrement number | `1` | `int`, `` | `
:dec`, `:dec:5`, `:dec:` |
| `mul` | Multiply number | `None` | `int`, `` | `
:mul:3`, `:mul:` |
| `div` | Divide number | `None` | `int`, `` | `
:div:2`, `:div:` |
| `rep` | Replace | `None` | `string`, `` | `
:rep:mystring`, `:rep:` |
| `del` | Delete | `None` | `None` | `
:del` |
| `swap` | Swap with another | `None` | `` | `
:swap:` |
| `append` | Append text | `None` | `string`, `int` | `
:append:_suffix`, `:append:123` |
| `prepend` | Prepend text | `None` | `string`, `int` | `
:prepend:prefix_`, `:prepend:v` |
| `upper` | Convert to uppercase | `None` | `None` | `
:upper` |
| `lower` | Convert to lowercase | `None` | `None` | `
:lower` |

## Installation 💻

### Using cargo

```bash
$ cargo install --git https://github.com/gbbirkisson/regop.git --tag 0.5.0
```

### Using install script

```bash
$ curl --proto '=https' --tlsv1.2 -LsSf https://github.com/gbbirkisson/regop/releases/download/0.5.0/regop-installer.sh | sh
```

### Download pre-built binaries

Go to the [latest release](https://github.com/gbbirkisson/regop/releases/latest) and download
the binary for your OS.

## Development 🚧

This is a regular rust project, so `cargo` will we enough. But if you want you can use
[just](https://github.com/casey/just):

```bash
$ just
Available recipes:
build # Build release
ci # Run CI pipeline
default # Show this help
dist # Recreate release.yml workflow
install # Install locally
lint-clippy # Run clippy linter
lint-fmt # Run fmt linter
run # Little test runs
test # Run tests
```