Skip to content

Join

Terminal window
splitby --join "<STRING>" [selection...]

By default, splitby tries to preserve the original delimiter between selections in field mode. --join replaces that behavior with your custom string.

Because delimiters can be regex, sometimes there are multiple delimiters in a record. Join also accepts a number of special keywords to help you choose which you want.

  • auto: Apply the default behaviour of trying after-previous, then trying before-next then falling back to a space.
  • first: Use the first delimiter in the record
  • last: Use the last delimiter in the record
  • after-previous: Use the delimiter after the previously printed field
  • before-next: Use the delimiter before the next printed field
  • space: Use a single space, equivalent to " "
  • none: Use an empty join, equivalent to ""

Join selections with a custom string:

Terminal window
echo "a,b,c" | splitby -d "," --join " | " 1 3
# a | c
Terminal window
echo "apple,banana,cherry" | splitby -d "," -j " -> " 1-3
# apple -> banana -> cherry

Use special keywords:

Tries to preserve original delimiters, falling back to space when no delimiter is available:

Terminal window
# When delimiters exist between selections, preserves them
echo "apple,banana,cherry" | splitby -d "," --join auto 1 3
# apple,cherry
# Falls back to space when no delimiter is available
echo "apple" | splitby -d "," --join auto 1 1
# apple apple

Uses the first delimiter found in the record:

Terminal window
echo "apple,banana;cherry:date" | splitby -d "/[,;:]/" --join first 3 4
# cherry,date

Uses the last delimiter found in the record:

Terminal window
# Multiple delimiter types - uses the last one found
echo "apple,banana;cherry:date" | splitby -d "/[,;:]/" --join last 1 2
# apple:banana

after-previous (delimiter after previous field)

Section titled “after-previous (delimiter after previous field)”

Uses the delimiter that immediately follows the previously selected field:

Terminal window
# Uses the comma after "apple" (field 1)
echo "apple,banana;cherry:date" | splitby -d "/[,;:]/" --join after-previous 1 3 1
# apple,cherry:apple

Uses the delimiter that immediately precedes the next selected field:

Terminal window
# Uses the comma before "cherry" (field 3)
echo "apple,banana;cherry:date" | splitby -d "/[,;:]/" --join before-next 1 4 2
# apple:date,banana

Concatenates selections with no separator between them:

Terminal window
echo "apple,banana,cherry" | splitby -d "," --join none 1 3
# applecherry

Always uses a single space, regardless of original delimiters:

Terminal window
# Original delimiter is comma, but output uses space
echo "apple,banana,cherry" | splitby -d "," --join space 1 3
# apple cherry
  • after-previous and before-next fallback to a space, if there is no delimiter available. I might change this depending on feedback.