2 个稳定版本
1.1.0 | 2021年10月18日 |
---|---|
1.0.1 |
|
#1014 in 文本处理
34KB
730 代码行
inslice
从文本块中提取特定的列和行是命令行中常见的一个任务,无论是从文件还是标准输入。然而,目前的方法,如 awk
或 tail
,并不是最直接或直观的,特别是它们通常需要特殊的语法或调用才能完成所需任务。
inslice
是一个用 Rust 编写的命令行工具,旨在解决这个问题,通过允许用户以文字和明确的方式使用列号和行号轻松地按列和行过滤输入文本。它由两个独立的二进制文件组成,分别是 colslc
和 rowslc
,分别用于列和行,可以一起使用以生成所需的输出。这遵循 Unix 哲学,即编写只做一件事并且做得很好的程序,并且可以一起工作。
要了解与现有等效命令的相似之处,请参阅比较。
安装
预构建的二进制文件
可以在本仓库的 发行版 部分找到为各种目标平台编译的预构建 colslc
和 rowslc
二进制文件。
Cargo
使用 cargo
工具链安装 colslc
和 rowslc
cargo install inslice
用法
colslc
colslc 1.0.0
Jace Tan <jaceys.tan@gmail.com>
A command-line utility for filtering input text by columns and writing them to standard output
USAGE:
colslc [OPTIONS] [--] [PATH]
ARGS:
<PATH>
Path to input file. To read from standard input, specify - as the path. If no path is
provided, the default behaviour will be to read from standard input
FLAGS:
-h, --help
Print help information
-V, --version
Print version information
OPTIONS:
-d, --delimiter <DELIMITER>
Optional delimiter to use for splitting input text into columns. If no delimiter is
provided, the default behaviour will be to split by any amount of whitespace
-f, --filters <FILTERS>...
Filters to be applied, using column numbers to denote which columns from the input text
should be retained. Multiple filters can be applied, the result of which is their union.
The following are accepted formats for filters, with column indexing starting from one,
beginning from the left-most column:
* [n] - an exact filter for selecting the n'th column
* [n:m] - a range-based filter for selecting the n'th to m'th (inclusive) columns
* [n:] - a range-based filter for selecting the n'th to last (inclusive) columns
* [:n] - a range-based filter for selecting the first to n'th (inclusive) columns
* [:n] - a range-based filter for selecting the first to last (inclusive) columns
Example:
`colslc - -f 1 4:6` will result in the 1st, 4th, 5th, and 6th columns of the input text
provided from standard input being written to standard output, separated by whitespace.
rowslc
rowslc 1.0.0
Jace Tan <jaceys.tan@gmail.com>
A command-line utility for filtering input text by rows and writing them to standard output
USAGE:
rowslc [OPTIONS] [--] [PATH]
ARGS:
<PATH>
Path to input file. To read from standard input, specify - as the path. If no path is
provided, the default behaviour will be to read from standard input
FLAGS:
-h, --help
Print help information
-V, --version
Print version information
OPTIONS:
-f, --filters <FILTERS>...
Filters to be applied, using row numbers to denote which rows from the input text should
be retained. Multiple filters can be applied, the result of which is their union. The
following are accepted formats for filters, with row indexing starting from one,
beginning from the top-most row:
* [n] - an exact filter for selecting the n'th row
* [n:m] - a range-based filter for selecting the n'th to m'th (inclusive) rows
* [n:] - a range-based filter for selecting the n'th to last (inclusive) rows
* [:n] - a range-based filter for selecting the first to n'th (inclusive) rows
* [:n] - a range-based filter for selecting the first to last (inclusive) rows
Example:
`rowslc - -f 1 4:6` will result in the 1st, 4th, 5th, and 6th rows of the input text
provided from standard input being written to standard output, separated by a newline.
比较
对于给定的输入文件
$ cat src/testdata/input.txt
REPOSITORY TAG IMAGE ID CREATED SIZE
vault 1.8.4 dc15db720d79 2 days ago 186MB
redis 6.2-alpine 6960a2858b36 3 days ago 31.3MB
postgres 14.0-alpine ae192c4d3ada 17 months ago 152MB
traefik 2.5 72bfc37343a4 18 months ago 68.9MB
colslc
awk
cat src/testdata/input.txt | awk '{ print $1, $4, $5, $6}'
等同于
cat src/testdata/input.txt | colslc -f 1 4:6
rowslc
head
cat src/testdata/input.txt | head -3
等同于
cat src/testdata/input.txt | rowslc -f :3
tail
cat src/testdata/input.txt | tail +3
等同于
cat src/testdata/input.txt | rowslc -f 3:
head
+ tail
cat src/testdata/input.txt | head -3 | tail +3
等同于
cat src/testdata/input.txt | rowslc -f 3
许可证
请参阅LICENSE。
依赖
~1.5MB
~23K SLoC