bsr
bsr

Reputation: 58662

sed search replace pattern

I have files with below text (and more).

import (
    "http"
    "web"
)

I want to replace all "web" with "pkg/web" (in all files). so the outcome needs to

 import (
        "http"
        "pkg/web"
    )

I try sed like

find . -type f -print0 | xargs -0 sed -i '/"web"/c\"pkg/web"'

which gives error.

sed: 1: "test" invalid command code .

what is the correct way?

Thanks.

Upvotes: 1

Views: 205

Answers (2)

sam
sam

Reputation: 2486

find . -type f -print0 | xargs -0 sed -i -e 's@"web"@"pkg/web"@g'

This works fine for me.

Upvotes: 0

anubhava
anubhava

Reputation: 785146

Problem is that you're using / as regex delimiter but also using / in your replacement string.

Good news is that sed allows different regex delimiters.

This sed with a different regex delimiter should work:

sed -i.bak 's~^\( *\)"web" *$~\1"pkg/web"~g'

UPDATE: To preserve whitespace on LHS of searched string:

find . -type f -print0 | xargs -0  sed -i '' 's~^\([[:space:]]*\)"web"[[:space:]]*$~\1"pkg/web"~g'

Upvotes: 1

Related Questions