Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sed code generator incorrectly deals with multiline expressions #2373

Open
Annonymus-v02 opened this issue Oct 29, 2024 · 0 comments
Open

Sed code generator incorrectly deals with multiline expressions #2373

Annonymus-v02 opened this issue Oct 29, 2024 · 0 comments
Labels

Comments

@Annonymus-v02
Copy link

Bug Description

Sed does not work as one might expect when the regex includes \n or otherwise spans multiple lines1, because it processes the input one line at a time, so if the expression matches multiple lines, sed never runs it over a large enough chunk of the input. This behavior is configurable — if one wishes to use multiline substitution sed can be made to keep more than one line at a time in memory, so it is not impossible to use multiline regexes with sed, but the code generator does not do so, instead it seems to only naively paste the regex and replacement into the s/// expression. I understand that it is not easy to generate the correct configuration for any possible regex, but the generator should not simply produce incorrect code either. If the correct command cannot be determined automatically, it could scan the expression for \n and emit an error instead of the command if found, and perhaps add a comment warning about the issue for any other expression, which might match a "hidden" newline through \s, \u000a, . or similar.

Reproduction steps

  1. Create a substitution regex (any flavor) such as \[heading]\nvalue = true with any replacement, for example [heading]\nvalue = false
  2. Open the sed code generator

Expected Outcome

A command which actually replaces matches of the given regular expression (I don't know sed well enough to craft one for the example expression I made)

Actual Outcome

A command which does not work, such as

sed -E 's/\[heading]\nvalue = true/[heading]\nvalue = false/gm;t' <<< "[heading]
value = true
"

To see that the given command does not work try it yourself or have a look at this online emulation: https://sed.js.org/?snippet=k7R59e

Browser

Firefox 130.0.1

OS

Windows 10 Enterprise 22H2 19045.4651
sed command tested on Ubuntu 22.04 under WSL

Footnotes

  1. I'm not actually very knowledgeable where sed is concerned, take everything I say about it here with a grain of salt and maybe somebody can correct me if I made a mistake. What I describe here stems simply from running into the same problem myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant