I’ve been developing a Fish function called tween. It’s a simple, flexible utility for extracting ranges of lines from files or STDIN input, and it’s flexible enough to handle just about any scenario you can throw at it, from numeric ranges, array style position/length ranges, or even string matching with regex capabilities.

You can find the source code in my Fish functions repository, specifically at tween.fish.

What it does

tween displays lines between a start and end point. The start and end can be line numbers, string matches, or regex patterns. It supports multiple ranges, relative offsets, and even works with piped input.

Basic usage

The simplest form is specifying line numbers:

tween file.txt 10 20

This displays lines 10 through 20 (inclusive). You can also use a dashed range format:

tween file.txt 10-20

And here’s the nice part: arguments can be in any order. Both of these work the same way:

tween file.txt 10 20
tween 10 20 file.txt

If you only specify a single line number, tween will display from that line to the end of the file:

tween file.txt 50

This shows lines 50 through the end of the file.

Multiple ranges

Need to extract several sections? Just separate them with commas:

tween file.txt 10-20,30-40
tween file.txt 10 20, 30 40

Both formats work, so use whichever feels more natural.

Relative offsets

Sometimes you know where you want to start but need to go a certain number of lines forward. Use +N for relative offsets:

tween file.txt 10 +20

This displays lines 10 through 30 (10 plus 20 lines). You can also count from the end using -N:

tween file.txt 50 -10

This shows lines 50 to 10 lines from the end of the file. There’s a special case: -1 means the end of the file (the last line):

tween file.txt 50 -1

This displays lines 50 through the end of the file. Other negative numbers like -2, -3, etc. still mean “N lines from the end” (second-to-last, third-to-last, etc.).

String matching

Instead of line numbers, you can match on strings. This is super useful when you know the content but not the exact line:

tween file.txt 'START' +20

This finds the line containing “START” and displays it plus the next 20 lines. You can also use string matching for both start and end:

tween file.txt 50-'END'

This shows lines 50 through the line containing “END”.

Regex patterns

For more complex matching, use regex patterns wrapped in slashes:

tween file.txt /START/ +20

This matches the regex pattern “START” and displays that line plus 20 more. You can also use the -r or --regex flag to treat all string arguments as regex:

tween -r file.txt 'foo' 'bar'

Both patterns are treated as regex when using the -r flag.

Exclusive mode

Sometimes you want the lines between two markers but not the markers themselves. Use -e or --exclusive:

tween -e file.txt 'BEGIN' 'END'

This displays all lines between (but not including) the lines containing “BEGIN” and “END”.

Syntax highlighting with bat

If you have bat installed, you can use it for syntax highlighting with the -b or --bat flag:

tween -b file.txt 10-20

This displays the range with syntax highlighting, which is especially nice when viewing code.

Piped input

tween works with piped input too. Just use - as the file argument:

cat file.txt | tween 10-20,30-40 -

This is handy when you’re already working with a pipeline.

Options summary

  • -e, --exclusive - Exclude the start and end lines from output
  • -b, --bat - Use bat instead of sed for syntax highlighting
  • -r, --regex - Treat all string arguments as regular expressions
  • -h, --help - Show help message

The function is flexible enough to handle most text extraction tasks, and the ability to mix line numbers, strings, and regex makes it incredibly versatile. Give it a try and see how it fits into your workflow!