bash-rex
A simple regexp executer for bash
Last updated 2 years ago by luanpotter .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install bash-rex 
SYNC missed versions from official npm registry.

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.

Current Tags

  • 0.7.2                                ...           latest (2 years ago)

9 Versions

  • 0.7.2                                ...           2 years ago
  • 0.7.1                                ...           2 years ago
  • 0.7.0                                ...           2 years ago
  • 0.6.0                                ...           2 years ago
  • 0.5.0                                ...           2 years ago
  • 0.4.0                                ...           2 years ago
  • 0.3.0                                ...           2 years ago
  • 0.2.0                                ...           2 years ago
  • 0.1.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 3
Dependencies (2)
Dev Dependencies (1)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |