• Stars
    star
    447
  • Rank 93,967 (Top 2 %)
  • Language
    Rust
  • License
    MIT License
  • Created about 6 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

xargs + awk with pattern matching support. `ls *.bak | rargs -p '(.*)\.bak' mv {0} {1}`

Rargs is kind of xargs + awk with pattern-matching support.

Crates.io Build Status

Installation

Mac OS

brew install rargs

Nix

nix-env -i rargs

(Currently available in unstable channel)

Binary

Download in the Release Page and put it in your PATH after uncompress.

Using Cargo

cargo install --git https://github.com/lotabout/rargs.git

Example usage

Batch rename files

Suppose you have several backup files whose names match the pattern <scriptname>.sh.bak, and you want to map each filename back to <scriptname>.sh. We want to do it in a batch, so xargs is a natural choice, but how do we specify the name for each file? I believe there is no easy way.

With rargs, however, you are able to do:

ls *.bak | rargs -p '(.*)\.bak' mv {0} {1}

Here {0} refers to the whole input line, while {1} refers to the first group captured in the regular expression.

Batch download

I had a bunch of URLs and their corresponding target filenames stored in a CSV file:

URL1,filename1
URL2,filename2

I hoped there was a simple way to download and save each URL with its specified filename. With rargs there is:

cat download-list.csv | rargs -p '(?P<url>.*),(?P<filename>.*)' wget {url} -O {filename}

Here (?P<group_name>...) assigns the name group_name to the captured group. This can then be referred to as {group_name} in the command.

AWK replacement?

Suppose you have an xSV file with lots of columns, and you only want to extract and format some of them, e.g.:

nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false

Here's an example of how rargs can be used to process it:

$ cat /etc/passwd | rargs -d: echo -e 'id: "{1}"\t name: "{5}"\t rest: "{6..::}"'
id: "nobody"     name: "Unprivileged User"       rest: "/var/empty:/usr/bin/false"
id: "root"       name: "System Administrator"    rest: "/var/root:/bin/sh"
id: "daemon"     name: "System Services"         rest: "/var/root:/usr/bin/false"

rargs allow you to specify the delimiter (regex) to split the input on, and allows you to refer to the corresponding fields or field ranges. This allows it to be used as an AWK replacement for some simple but common cases.

How does it work?

  1. receive the input on stdin and split it into lines
  2. split (-d) or extract (-p) the input into named or numbered groups, with {0} matching the whole line
  3. map the named and numbered groups into a command passed as the remaining arguments, and execute the command

Features

Regexp captures

rargs allows you to use any regular expression to match the input, and captures anything you are interested in. The syntax is the standard, mostly Perl-compatible Rust regex syntax used by tools such as ripgrep.

  • positional (numbered) groups are captured with parentheses, e.g. '(\w+):(\d+)', and the corresponding groups are referred to by {1}, {2} etc. in the command
  • named groups are captured with (?P<name>...) and referred to by {name} in the command

Delimiter captures

For simple usage, you might not want to write the whole regular expression to extract parts of the line. All you want is to split the groups by some delimiter. With rargs you can achieve this by using the -d (delimiter) option.

Field ranges

We already know how to refer to captures by number ({1}) or by name ({name}). There are also cases where you might want to substitute multiple fields at the same time. rargs also supports this with field-range expressions.

Suppose we have already captured 5 groups representing the strings 1, 2, 3, 4 and 5

  • {..} gathers them all into 1 2 3 4 5 (note that they are separated by a space; this can be overridden by the -s option)
  • {..3} results in 1 2 3
  • {4..} results in 4 5
  • {2..4} results in 2 3 4
  • {3..3} results in 3

You can also specify a "local" separator (which will not affect the global setting):

  • {..3:-} results in 1-2-3
  • {..3:/} results in 1/2/3

Negative field

Sometimes you may want to refer to the last few fields but have no way to predict the total number of fields of the input. rargs offer negative fields.

Suppose we have already captured 5 groups representing the strings 1, 2, 3, 4 and 5:

  • {-1} results in 5
  • {-5} results in 1
  • {-6} results in nothing
  • {-3..} results in 3 4 5

Multiple threading

You can run commands in multiple threads to improve performance:

  • -w <num> specifies the number of workers you want to run simultaneously
  • -w 0 defaults the number of workers to the number of CPUs on your system

Special Variables

  • {LINENUM} or {LN} to refer to current line number.

Interested?

All feedback and PRs are welcome!

More Repositories

1

skim

Fuzzy Finder in rust!
Rust
4,755
star
2

write-a-C-interpreter

Write a simple interpreter of C. Inspired by c4 and largely based on it.
C
3,667
star
3

Let-s-build-a-compiler

A C & x86 version of the "Let's Build a Compiler" by Jack Crenshaw
C
441
star
4

fuzzy-matcher

Fuzzy Matching Library for Rust
Rust
234
star
5

very-simple

A very simple theme for hexo
SCSS
148
star
6

skim.vim

vim support for skim
Vim Script
135
star
7

tuikit

Tool kit for writing TUI applications in Rust.
Rust
101
star
8

hexo-theme-noise

A hexo theme
Less
98
star
9

CodeGenerator

Intellij IDEA Plugin for creating customized code generators like the builtin toString, equals, etc.
Java
50
star
10

spring-security-example

REST authentication apis & token based authentication, etc.
Java
30
star
11

transformer-playground

Annotation processor @Transform for creating transformers for classes.
Java
16
star
12

pymustache

Mustache template engine from scratch in Python.
Python
16
star
13

static-wiki

CSS
13
star
14

SlackGuide-cn

A chinese guide for slackware based on SlackBook.
10
star
15

dotfiles

Manage the resource files under home folder
Emacs Lisp
8
star
16

lotabout.github.io

My personal blog. (source at source branch)
C++
8
star
17

buddy-system

Simple implementation of a buddy system for memory management.
C
8
star
18

orgwiki

personal wiki generated by org-mode
CSS
8
star
19

compiler-design-in-c

Codes for book <compiler design in C>
C
6
star
20

ywvim

a fork of ywvim. http://www.vim.org/scripts/script.php?script_id=2662
Vim Script
6
star
21

vimwiki-tpl

template for vimwiki
CSS
5
star
22

axe

Handy utilities for racket
Racket
5
star
23

simple-template-engine

A simple template engine written in python
Python
5
star
24

mdbook-fix-cjk-spacing

mdbook preprocessor that removes extra space rendered for Chinese lines.
Rust
4
star
25

c-interfaces

codes for book "C Interfaces and Implementation Techniques".
C
4
star
26

underscore-comment

Read and Comment on source code of underscore.js
JavaScript
3
star
27

hexo-filter-fix-cjk-spacing

Join continuous CJK lines in markdown in Hexo.
JavaScript
3
star
28

project-euler-racket

Project Euler in racket.
Racket
3
star
29

simple-framework

A simple python web framework from scratch
Python
3
star
30

jasypt-online

Jasypt 在线加解密工具
HTML
2
star
31

nikola-bnw

A nikola theme.
CSS
2
star
32

zzz

theme for nikola
CSS
2
star
33

ideas

log for new ideas and the execution of ideas
2
star
34

cup

A small web framework for racket
Racket
2
star
35

learn-c-the-hard-way

code & exercise reading book <Learn C the hard way>
C
2
star
36

curtail

pipe stdin to a fixed-size log file
Rust
2
star
37

zlex

A lexical analyzer generator.
C
2
star
38

build-your-own-pytorch

Understand deep learning framework(like torch) by implementing the essentials
Python
2
star
39

pymkd

Markdown in python
Python
1
star
40

cljs-douban

douban.fm in CLJS+Electron
Clojure
1
star
41

lemon

Lemon parser generator (http://www.hwaci.com/sw/lemon/). Submit for code review for study purpose.
C
1
star
42

code-snippets

collection of useful code snippets.
C
1
star
43

my-time

A time management web application written in clojure with luminus.
Clojure
1
star
44

cljs-douban-reframe

douban.fm in CLJS+Electron+Re-frame
Clojure
1
star
45

my-slackbuilds

My slackware build scripts
Shell
1
star
46

pscat

socat in one python file
Python
1
star
47

orgmark.vim

Vim Script
1
star
48

docker-slackware

Build slackware images for docker
1
star
49

LiSP

Codes for book "Lisp in Small Pieces"
Scheme
1
star
50

java-spi-playground

A simple expression interpreter that use SPI to load user-customized functions
Java
1
star
51

dragon-book-notes

Notes reading the Dragon Book.
C
1
star
52

dwm

My own customization of dwm, starting with dwm-6.0.
C
1
star
53

book

The Rust Programming Language
Rust
1
star