bash-rex

0.7.2 • Public • Published

rex

A simple regexp executer for bash.

Like sed, but you don't need to deal with complex escape characters.

It uses node regexp. Could not be simpler to use!

Installation

You can install using npm:

npm i -g bash-rex

Or yarn:

yarn global add bash-rex

Or download the bin from github (it comes with node bundled in, so it runs on any x64 linux box):

wget https://github.com/luanpotter/rex/raw/master/dist/rex
chmod +x rex
sudo mv rex /usr/bin/rex

The bin bundle is a bit heavy because of the node inclusion, so if you have node, prefer the former methods as they are really, really lightweight (1.3K).

Usage

Very simple! Just run:

echo 'foo' | rex 'o' 'e'
> fee

But unlike sed, you don't need to escape complex regex'es:

echo 'package-name-1.2.3' | rex '(\w)-\d[\d-.:+]*' '$1'
> package-name

Just use single quotes so bash won't replace stuff like $1.

If you want to change a file, you can use sponge:

echo 'foo' > file
cat file | rex 'foo' 'bar' | sponge file
cat file

Install sponge for Arch Linux:

sudo pacman -S moreutils

No escape!

Almost nothing needs to be escaped.

The first parameter is a pure node regex, that will be run with the flags 'mg' (multiline and global). More information here.

The second parameter will be the replacement literally, except for:

  • $n, where n is a number, will become the n-th capture group (starting on $1).
  • \t will become tab, \n will become newline, \r will become carriage return, \$ will become $ and \\ will become \

More options

It's supposed to be simple: if you want simple, stop reading. If you want a few more options, there are flags:

  • -h : if present, won't run, just show the help section
  • -f : if present, the stdin becomes a new-line separated list of files, and rex will perform the replace on those files
  • -b : if present, the backup mode is active:
    • if not on -f, rex will output stdin as is, but will save the changes to a __rex__.bak file with the requested changes
    • if on -f, it will replace on every file specified, but the originals will be saved on *.bak files alongside the altered files

Beware when using the -f flag: rex will open every file as UTF-8. If you pipe binary files to rex, it will mess them up. Always use something like find, grep or ag to pipe only relevant files.

There are more examples here.

Package Sidebar

Install

npm i bash-rex

Weekly Downloads

0

Version

0.7.2

License

MIT

Unpacked Size

5.66 kB

Total Files

4

Last publish

Collaborators

  • luanpotter