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 ?
sed
replaces by using the sed 's/pattern/replacement/'
syntax. In your case, you were missing the last /
. So by saying this it will work:
sed '11s/.*/x.y.z.zz=\/a\/b\/e\/d\//' file
^
However, it may be cleaner to use another delimiter, so that the syntax is more clear. What about #
? (It can also be ~
, _
, etc.):
sed '11s#.*#x.y.z.zz=/a/b/e/d/#' file
Test
$ cat a
a
x.y.z.zz=/a/b/c/d/
b
c
Let's replace line 2:
$ sed '2s#.*#x.y.z.zz=/a/b/e/d/#' a
a
x.y.z.zz=/a/b/e/d/
b
c
You can just replace c
with e
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
with e
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
, executing sed s/c/e/
will have output x.y.z.zz=/a/b/e/d/c
If all the occurrences need to be changed g
(global) needs to be added in sed
command
e.g. sed s/c/e/g
will give output x.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 the sed
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