bash-rex v0.7.2
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-rexOr yarn:
yarn global add bash-rexOr 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/rexThe 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'
> feeBut unlike sed, you don't need to escape complex regex'es:
echo 'package-name-1.2.3' | rex '(\w)-\d[\d-.:+]*' '$1'
> package-nameJust 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 fileInstall sponge for Arch Linux:
sudo pacman -S moreutilsNo 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).\twill become tab,\nwill become newline,\rwill 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__.bakfile with the requested changes
- if not on
- if on
-f, it will replace on every file specified, but the originals will be saved on*.bakfiles alongside the altered files
- if on
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.