0

In a file a.py I have multiple lines which look like this:

with open('./260/U') as infile, open('./260/Uout', 'w') as outfile:

I am trying to search and replace all of the /260/ directories with $dir using sed:

dir=200
sed -i 's+\/.*?\/+\/'"$dir"'\/+g' "./a.py"

However my attempt does not modify the file. How do I replace all of the /xxxx/ (i.e an unknown string between two slashes, in this case /260/) with /$dir/?

Tom Waits
  • 161
  • 2
    Sed doesn't support the .*? non greedy match, and you don't need to escape / since you're using + as the delimiter. So try 's+/[^/]*/+/'"$dir"'/+g' – steeldriver Oct 27 '20 at 12:31
  • In general, I suggest you to use the os python module and have readable, customizable code, and modify the path whenever you want without any sed. – thanasisp Oct 27 '20 at 13:06

0 Answers0