greymd / teip

Masking tape to help commands "do one thing well"
MIT License
569 stars 19 forks source link

Multiline flag doesn't seem to work #28

Closed balupton closed 2 years ago

balupton commented 2 years ago

I'm assuming this uses the rust regex engine:

https://docs.rs/regex/latest/regex/#grouping-and-flags

Of which the following should work right?

echo $'a\nb\nc\na\nb\nc' | teip -g '(?m:\nb$)'

Such that I can do:

echo $'a\nb\nc\na\nb\nc' | teip -g '(?m:\nb$)' -- sd '.*' ''

to get back:

a
c
a
c

As such works with sd

echo $'a\nb\nc\na\nb\nc' | sd '(?m:\nb$)' ''
greymd commented 2 years ago

This is the expected behavior. Unfortunately, I have no plans to support the behavior in the future.

If you hope to match with multi-line mode, please use the -z option like this.

$ echo $'a\nb\nc\na\nb\nc' | teip -z -g '\nb' -o
a[
b]
c
a[
b]
c

Let me explain the technical background. teip can give only the matched parts to another command. In this case, each occurence is concatenated with a newline and given as standard input. Therefore, if the single occurence contains newlines, it will cause unexpected behavior, so the flag is set to a fixed value.

When the -z option is used, the NUL character is used as the concatination character, so it is controlled to avoid problems (see README for detailed behavior).