7 个稳定版本
1.3.4 | 2022 年 4 月 23 日 |
---|---|
1.3.3 | 2021 年 7 月 27 日 |
1.3.1 | 2020 年 8 月 13 日 |
1.3.0 | 2020 年 6 月 26 日 |
#442 在 命令行工具
每月 111 次下载
80KB
2K SLoC
Choose
这是 choose
,一个比 cut
和 (有时) awk
更人性化和快速的替代品
特性
- 简洁的字段选择语法,类似于 Python 列表切片
- 从行尾开始的负索引
- 可选的起始/结束索引
- 从 0 开始索引
- 反转范围
- 对于足够长的输入,比
cut
略快,比awk
快得多 - 使用 Rust 的正则表达式语法进行正则表达式字段分隔符
原理
AWK 编程语言是为文本处理设计的,在这个领域非常强大。然而,awk
命令并不适合快速在 shell 中使用,因为即使是最简单的程序也需要用花括号引起来进行引号引用
awk '{print $1}'
同样,cut
也不适合快速在 shell 中使用,因为其语法令人困惑。字段分隔符和范围很难在第一次尝试中就正确设置。
因此,我向您介绍 choose
。它并不打算取代上述工具,而是一个简单直观的工具,当使用 awk
或 cut
的基本功能时,但不需要这些工具的额外开销时,可以随手拿来使用。
贡献
请参阅我们位于 contributing.md 的指南。
用法
$ choose --help
choose 1.2.0
`choose` sections from each line of files
USAGE:
choose [FLAGS] [OPTIONS] <choices>...
FLAGS:
-c, --character-wise Choose fields by character number
-d, --debug Activate debug mode
-x, --exclusive Use exclusive ranges, similar to array indexing in many programming languages
-h, --help Prints help information
-n, --non-greedy Use non-greedy field separators
-V, --version Prints version information
OPTIONS:
-f, --field-separator <field-separator>
Specify field separator other than whitespace, using Rust `regex` syntax
-i, --input <input> Input file
-o, --output-field-separator <output-field-separator> Specify output field separator
ARGS:
<choices>... Fields to print. Either a, a:b, a..b, or a..=b, where a and b are integers. The beginning or end
of a range can be omitted, resulting in including the beginning or end of the line,
respectively. a:b is inclusive of b (unless overridden by -x). a..b is exclusive of b and a..=b
is inclusive of b
示例
choose 5 # print the 5th item from a line (zero indexed)
choose -f ':' 0 3 5 # print the 0th, 3rd, and 5th item from a line, where
# items are separated by ':' instead of whitespace
choose 2:5 # print everything from the 2nd to 5th item on the line,
# inclusive of the 5th
choose -x 2:5 # print everything from the 2nd to 5th item on the line,
# exclusive of the 5th
choose :3 # print the beginning of the line to the 3rd item
choose -x :3 # print the beginning of the line to the 3rd item,
# exclusive
choose 3: # print the third item to the end of the line
choose -1 # print the last item from a line
choose -3:-1 # print the last three items from a line
编译和安装
从源代码安装
为了构建 choose
,您需要安装 Rust 工具链。您可以在 这里 找到说明。
然后,为了安装
$ git clone https://github.com/theryangeary/choose.git
$ cd choose
$ cargo build --release
$ install target/release/choose <DESTDIR>
只需确保 DESTDIR 在您的路径中。
使用软件包管理器安装
Cargo
$ cargo install choose
Arch Linux
$ yay -S choose-rust-git
Fedora/CentOS COPR
$ dnf copr enable atim/choose
$ dnf install choose
Homebrew
$ brew install choose-rust
MacPorts
$ sudo port install choose
性能测试
性能测试使用的是 bench
工具。
性能测试基于以下假设:在 test/
目录中有五个文件与“long*txt”的通配符匹配。GitHub不支持在常规仓库中存储足够大的文件,但为了参考,我正在使用的文件长度如下
1000 test/long.txt
19272 test/long_long.txt
96360 test/long_long_long.txt
963600 test/long_long_long_long.txt
10599600 test/long_long_long_long_long.txt
内容大致如下
Those an equal point no years do. Depend warmth fat but her but played. Shy and
subjects wondered trifling pleasant. Prudent cordial comfort do no on colonel as
assured chicken. Smart mrs day which begin. Snug do sold mr it if such.
Terminated uncommonly at at estimating. Man behaviour met moonlight extremity
acuteness direction.
Ignorant branched humanity led now marianne too strongly entrance. Rose to shew
bore no ye of paid rent form. Old design are dinner better nearer silent excuse.
She which are maids boy sense her shade. Considered reasonable we affronting on
expression in. So cordial anxious mr delight. Shot his has must wish from sell
依赖项
~6MB
~104K SLoC