regexsedreplacematchpositive-lookahead

Convert regex positive look ahead to sed operation


I would like to sed to find and replace every occurrence of - with _ but only before the first occurrence of = on every line.

Here is a dataset to work with:

ke-y_0-1="foo"
key_two="bar"
key_03-three="baz-jazz-mazz"
key-="rax_foo"
key-05-five="craz-"

In the end the dataset should look like this:

ke_y_0_1="foo"
key_two="bar"
key_03_three="baz-jazz-mazz"
key_="rax_foo"
key_05_five="craz-"

I found this regex will match properly.

\-(?=.*=)

However the regex uses positive lookaheads and it appears that sed (even with -E, -e or -r) dose not know how to work with positive lookaheads.

I tried the following but keep getting Invalid preceding regular expression

cat dataset.txt | sed -r "s/-(?=.*=)/_/g"

Is it possible to convert this in a usable way with sed?

Note, I do not want to use perl. However I am open to awk.


Solution

  • You can use

    sed ':a;s/^\([^=]*\)-/\1_/;ta' file
    

    See the online demo:

    #!/bin/bash
    s='ke-y_0-1="foo"
    key_two="bar"
    key_03-three="baz-jazz-mazz"
    key-="rax_foo"
    key-05-five="craz-"'
    sed ':a; s/^\([^=]*\)-/\1_/;ta' <<< "$s"
    

    Output:

    ke_y_0_1="foo"
    key_two="bar"
    key_03_three="baz-jazz-mazz"
    key_="rax_foo"
    key_05_five="craz-"
    

    Details: