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
  • 713,503
  • 59
  • 514
  • 593
Radex
  • 6,303
  • 18
  • 44
  • 76

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
  • 713,503
  • 59
  • 514
  • 593