Skip to content

Basics

splitby splits text into fields and returns just the pieces you ask for.

Terminal window
echo "apple,banana,cherry" | splitby , 2
# banana

Delimiters are search strings that we split the text by. You can pass one explicitly:

Terminal window
echo "a b c" | splitby -d " " 2
# b

Or implicitly drop the flag, and splitby will detect it:

Terminal window
echo "a,b,c" | splitby , 1 3
# a,c

Regex delimiters are also supported, by surrounding the string with forward slashes:

Terminal window
echo "a b c" | splitby -d "/\\s+/" 2
# b

Selections are one-based indexes:

Terminal window
echo "a b c d" | splitby -d " " 1
# a

Ranges work too:

Terminal window
echo "a b c d" | splitby -d " " 2-3
# b c

Negative indexes count from the end:

Terminal window
echo "a b c d" | splitby -d " " -1
# d

You can list multiple selections with spaces:

Terminal window
echo "a b c d" | splitby -d " " 1 3-4
# a c d