I wish to replace
x.y.z.zz=/a/b/c/d/
with
x.y.z.zz=/a/b/e/d/
i know x.y.z.zz
in advance.I also know the line number in advance.
I have tried this
sed "11s/.*/x.y.z.zz=\/a\/b\/e\/d\/" filename
but this is giving error. Is there a better way to directly search and replace the string ?
You can just replace
c
withe
if you know your input will always have "x.y.z.zz=/a/b/c/d".e.g. just executing
sed s/c/e/
will just replace
c
withe
in the line. Also, you don't need to change the complete line always. You can just change a character or a word in the text.Additionally, if a line contains more than one occurrence of character/word, this command will only change the first one
e.g. if input string is
x.y.z.zz=/a/b/c/d/c
, executingsed s/c/e/
will have outputx.y.z.zz=/a/b/e/d/c
If all the occurrences need to be changed
g
(global) needs to be added insed
commande.g.
sed s/c/e/g
will give outputx.y.z.zz=/a/b/e/d/e
If
sed
needs to be executed only for a particular line, line number shall be mentioned in thesed
command itself, as done in the question.This is the link (http://www.grymoire.com/Unix/Sed.html), I always refer when in question with
sed
sed
replaces by using thesed 's/pattern/replacement/'
syntax. In your case, you were missing the last/
. So by saying this it will work:However, it may be cleaner to use another delimiter, so that the syntax is more clear. What about
#
? (It can also be~
,_
, etc.):Test
Let's replace line 2: