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" .
anubhava
  • 761,203
  • 64
  • 569
  • 643
Radex
  • 7,815
  • 23
  • 54
  • 86

1 Answers1

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/"
anubhava
  • 761,203
  • 64
  • 569
  • 643