1

I have a folder containing multiple files, each file contains a string like

  "tree": "/a/anything-here/b/"

for each file I need to replace the content between inner "//" in this case "anything-here" with a string

I am using sed command with no success, could you help me?

sed -i 's/"root": a/b" .
1
  • 1
    Looks like a JSON field? Don't use non-syntax aware parsers like sed in case Commented Oct 30, 2020 at 7:16

1 Answer 1

2

You may use this sed:

s='"tree": "/a/anything-here/b/"'
sed -E 's~"(/[^/]*/)[^/]*/~\1new-string/~' <<< "$s"
"tree": /a/new-string/b/"

Or using awk:

awk -v str='new-string' 'BEGIN{FS=OFS="/"} {$3 = str} 1' <<< "$s"
"tree": "/a/new-string/b/"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.