Content
Description
Reads text input from stdin or files to split into parts by a given separator to extract the values and return the chosen range.
Usage
Think of parts like Go's support for ”slice” operator or Python with the syntax slice[low:high]. The range parameter is a deviation of this.
Range index start at 0 and ends at the max occurence. Take these range addresses, for example:
0 # First part.
1 # Second part.
2:4 # Third and forth parts.
:4 # Get all from beginning until the forth part.
3: # Forth part until the end
3:-1 # Forth part until second to last.
You can get the allowed range by using the -m / --max option.
echo "ABC" | parts 1: -m
[0:4]
Simple example that consists of the string "ABC\n" and used with the default divider (-c / --character).
echo "ABC" | parts 1:
BC
echo "ABC" | parts :
ABC
# slices the newline
echo "ABC" | parts 2:-1
C
echo "ABC" | parts 2:
C
echo "ABC" | parts 1:-2
B
More complex examples from our source code tree. All examples do the same in different ways with the goal of extracting the license name from SPDX headers:
# split by emtpy-line with file main.go to extract the first part (0) / piped to space divider returning the second to last part
parts -e -f main.go 0 | parts -s -1
BSD-3-Clause
# divide by the value "SPDX" with file main.go to get the third part (2) / piped to space divider returning the second part (1)
parts -d SPDX -f main.go 2 | parts -s 1
BSD-3-Clause
# get the charaters 79 until 91 from file main.go
parts -f main.go 79:91
BSD-3-Clause
Features
Application
- Slice text like Go string from stdin or from file(s).
- Split by character (default), spaces, emtpy-lines, newlines or custom words.
- Powerful asset in itself or in combination with other tools for string manipulation on the command line.
Security
Linux/musl Build |
Linux Runtime¹² |
OpenBSD Runtime² |
- Static Build ✓
- Full RELRO ✓
- NX enabled ✓
- PIE enabled ✓
- No RPATH ✓
- No RUNPATH ✓
- No Symbols ✓
|
- Capabilities drop ✓
- Seccomp Filters ✓
- Landlock enabled ✓
- No New Privileges flag ✓
- Chroot-able³ ✓
|
- Pledged ✓
- Unveiled ✓
- Chroot-able ✓
|
³ Needs access to /proc/self.
Download
Static binary for amd64 architecture:
Installation
Extract the binary from the downloaded archive.
tar -xf parts.tar.zst parts
./parts
You should place it in a directory that is included in the environment variable $PATH.
Options
-f | --file FILENAME | Use FILENAME as input. [multi]
-m | --max | Show max number of parts available for given separator, ignoring your range.
Separators
-d | --divider SEPARATOR | Divide input by SEPARATOR string or options below.
-c | --character | Split by every character. [default]
-e | --empty-line | Split by every empty-line.
-n | --new-line | Split by every new-line.
-s | --space | Split by spaces/fields.
Attribution
Security Libraries
Other Libraries
See --attribution option on the command line for more.
License
BSD-3-Clause License © 2023 Antonino Catinello