Split input string(s) by delimiter and print each item as a separate line.
strsplit [OPTIONS] ARGS...If ARGS are provided, each argument is treated as one input line (one run).
If no argument is specified, input is read from stdin, line by line (each line is one run).
For each run:
- Take input string
- Optionally case-fold
- Split by delimiter (default: whitespace
\s+) - Optionally sort
- Print items according to format rules
A run is defined as:
- One command-line argument, OR
- One line read from stdin
# Install build dependencies
sudo apt install meson ninja-build gcc
# Configure and build
meson setup builddir
cd builddir
ninja
# Install (optional)
sudo ninja install# Build Debian package
dpkg-buildpackage -us -uc
# Install the package
sudo dpkg -i ../strsplit_*.deb-d, --delim REGEX
Use POSIX Extended Regular Expression as delimiter.
Default: \s+
Delimiter matches are not included in output.
-n, --linenum
Print line number before each split item.
Default numbering base: 1
-0, --0-based
Use 0-based numbering.
-z, --reset
Reset numbering between runs.
Default behavior:
- If multiple runs and
-znot specified → numbering continues across runs - If
-zspecified → numbering restarts per run
-c, --format FMT
Custom output format.
If -n is enabled:
- Default format:
"%d. %s"
If -n is not enabled:
- Default format:
"%s"
Format placeholders:
| Placeholder | Meaning |
|---|---|
%d |
item index |
%s |
item string |
Undefined placeholders → implementation-defined (may pass to printf).
-N, --insert-nl
Insert one blank line between runs.
May be specified repeatedly:
-N→ 1 blank line-NN→ 2 blank lines- etc.
No trailing blank lines after last run.
-i, --indented
Indent output by 2 spaces per level.
Repeatable:
-i→ 2 spaces-ii→ 4 spaces
Indentation applies to each output line.
Sorting applies after splitting and before printing.
-r, --reversed
Reverse final ordering.
-f, --ignore-case
Case-insensitive lexicographic sort
-s, --numeric-sort
Compare using strtol()
-g, --general-numeric-sort
Compare using strtod()
-H, --human-numeric-sort
Human numeric (e.g. 2K < 10K)
-D, --dictionary-sort
Compare ignoring non-alphanumeric chars
-V, --version-sort
Version-aware sort (e.g. 1.9 < 1.10)
If multiple sort modes specified → last one wins.
If no sort option specified → preserve original order.
For each run:
- Read input
- If
-fspecified → fold case (implementation:toupper()) - Split using delimiter regex
- Preserve empty items (if delimiter produces empty tokens)
- Apply selected sort (if any)
- Apply reverse (if
-r) - Output with formatting
- Insert run-separator newlines (if
-Nand not last run)
| Code | Meaning |
|---|---|
| 0 | success |
| 1 | invalid arguments |
| 2 | runtime error |
strsplit "foo bar baz"Output:
foo
bar
baz
strsplit -z0niN 'foo bar' 'cat dog'Output:
0. foo
1. bar
0. cat
1. dog
strsplit -sn "10 2 30 4"Output:
1. 2
2. 4
3. 10
4. 30
strsplit -V "1.2 1.10 1.3"Output:
1.2
1.3
1.10
strsplit -d "," "foo,bar,baz"Output:
foo
bar
baz
echo "foo bar baz" | strsplitOutput:
foo
bar
baz
- Empty input line → no output
- Delimiter matching entire string → no output
- Large input → dynamic realloc growth
- UTF-8 handling → byte-wise unless extended
Source the completion script or install it to your bash completion directory:
source bash-completion/strsplitOr copy to system completion directory:
sudo cp bash-completion/strsplit /etc/bash_completion.d/Copyright (C) 2026 Lenik [email protected]
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.
Lenik [email protected]
Please report bugs and feature requests at: https://github.com/lenik/strsplit/issues