I'm trying to modifying a path stored in a variable ($var
) through sed
. In truth, I need to replace $(dirname "$var")
for reasons related to the purpose of my script.
An example of var
is var=/dir/dir xyz/file.txt
, while filename
contains several paths to be substituted which could be in any position (once near the middle of the line, another at the beginning, etc). Below I try to give an example of filename
:
dog foo/bar /dir/dir xyz/file.txt
a b c d /dir/dir xyz/file.txt x y z
/dir/dir xyz/file.txt 1234
{[(/dir/dir xyz/file.txt
What I want is
dog foo/bar .
a b c d . x y z
. 1234
{[(.
I tried the following
sed "s|"$(dirname "$var")"|.|g" "$filename"
obtaining sed: -e expression #1, char 31: unterminated `s' command
Could you help me? Of course, you could suggest another way besides sed
.
""
)" -- yes. – ilkkachu Mar 27 '23 at 13:53.*
, or backslash, or the|
you used as separator – ilkkachu Mar 27 '23 at 13:53$(dirname "$VAR")
– user9952796 Mar 27 '23 at 14:06--in-place
while testing or in your question - add that on your own after you get an answer to your problem if you like as it's completely irrelevant and so just clutters your question. – Ed Morton Mar 27 '23 at 14:10VAR
? How are you setting it? Show how you run it. Show what you're doing and what happens. Would the output ofdirname
ever be/dir/dir xyz/file.txt
? – ilkkachu Mar 27 '23 at 14:12