user4933588
user4933588

Reputation:

Command to replace a string with another string with a slash

I can't find the mistake in my command. I want to replace

/home/data/newfiles with /home/data/data1/newfiles

I used

 sed -i 's/data/data/data1/g' filename

but it is not working.

Upvotes: 1

Views: 37

Answers (2)

Ed Morton
Ed Morton

Reputation: 203209

$ echo '/home/data/newfiles' | sed 's#\(/home/data\)\(/newfiles\)#\1/data1\2#'
/home/data/data1/newfiles

Upvotes: 1

choroba
choroba

Reputation: 241768

The replacement string can't contain the separator unbackslashed (neither can the regular expression, but that's not a problem here).

sed -i 's/data/data\/data1/g' filename

or, use a different separator

sed -i 's=data=data/data1=g' filename

Upvotes: 0

Related Questions