parts

command module
v0.0.0-...-992657a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 29, 2025 License: BSD-3-Clause Imports: 12 Imported by: 0

README

Content

Description

Reads text input from stdin or files and splits it into parts by a given separator to extract values and return the chosen range.

Usage

If you're a programmer, then think of parts as a command line adoption of Go's support for the ”slice” operator or Python with the syntax slice[low:high]. The used range parameter is a deviation of this representation.

As a user you have to grasp that the range specifies how to slice a sequence of text. You can specify where to start the slicing with the first value, and where to end with the last. You can also specify the steps by using the separator.

The parts command is special as it allows you to extract mutliple ranges from the whole thing at once using the same separator or by piping the output to manipulate the value with third party tools.

Range index start at 0 and ends at the maximal occurence. Take these range addresses as a reference:

  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, the range itself is going to be omitted.

  echo "ABC" | parts 1: -m
  [0:4]

Simple examples that consist of the string "ABC\n" and are piped to parts that is used with the default separator (-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

  echo -n "ABC" | parts 1: 0
  BCA

More complex examples using main.go from the source code tree. All examples do the same in different ways with the goal of extracting the license name from SPDX headers at line 2:

  # 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

  # catch the second line in main.go / split the piped output by - and catch parts 2 3 4 / split by space and return the second part (1)
  parts -n -f main.go 1 | parts -d - 2 3 4 | parts -s 1
  BSD-3-Clause

  # simple space separation
  parts -f main.go -s 7
  BSD-3-Clause

  # variation with –
  parts -f main.go 79:82 83 85:91 -g –
  BSD–3–Clause

Features

Application
  • Slice text from stdin or from file(s).
  • Split the input by character (default), spaces, emtpy-lines, newlines or even custom words or sequence.
  • Retrieve the possible range for your chosen separator with the -m/--max option in advance.
  • Get multiple parts in one call and glue it together, even with custom strings.
  • 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]
  -g | --glue               | Use this string to concat the results of the ranges.
                            | Defaults to the chosen separator, if not set explicitly.
  -m | --max                | Show max number of parts available for given separator, ignoring your range.
  --max-number              | Show max (see above) as plain number.
  --help                    | Show this help.
  --attribution             | Attribute to dependencies and requirements.
  --license                 | License and copyright statement.
  --version                 | Print version.
Separators
  -c | --character          | Split by every character. [default]
  -d | --divider SEPARATOR  | Divide input by SEPARATOR string or options below.
  -e | --empty-line         | Split by every empty-line.
  -n | --new-line           | Split by every new-line.
  -s | --space              | Split by spaces/fields.
  -N | --null               | Split by NUL character.

Attribution

Security Libraries
Other Libraries

See --attribution option on the command line for more.

License

BSD-3-Clause License © 2023 Antonino Catinello

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL