To replace a string in vi editor use global replace command,
:%s/searchstring/replacestring/g
this statement will replace searchstring and replace it with replacestring.
One more example,
:%s/hello/hi/g
Here this command will replace string hello with hi in the entire text.
Above is fairly simple and most of us who work on vi are well versed with the command above. Problem is when you want to remove ^M and replace a directory structure with another one.
Consider replacing a directory /home/applmgr with /usr/var in a text file,
:%s/\/home\/applmgr/\/usr\/var/g
here use \ backslash to nullify / forward slash.
Following command will replace ^M,
:%s/^v^M//g
here ^v is CTRL+V and ^M is CTRL+M which after entering in vi looks like below,
:%s/ ^M//g
:%s/searchstring/replacestring/g
this statement will replace searchstring and replace it with replacestring.
One more example,
:%s/hello/hi/g
Here this command will replace string hello with hi in the entire text.
Above is fairly simple and most of us who work on vi are well versed with the command above. Problem is when you want to remove ^M and replace a directory structure with another one.
Consider replacing a directory /home/applmgr with /usr/var in a text file,
:%s/\/home\/applmgr/\/usr\/var/g
here use \ backslash to nullify / forward slash.
Following command will replace ^M,
:%s/^v^M//g
here ^v is CTRL+V and ^M is CTRL+M which after entering in vi looks like below,
:%s/ ^M//g
Comments