In this post I am sharing the trick to edit file without opening it. To achieve this task we will use vi editor. vi is very well known,popular,ultimate and awesome editor and by default available in all Unix/Linux operating system.
vi -c 'COMMAND|wq' /path/of/file
Example: We will replace a keyword with new keyword in file. I hope you know that in vi editor to replace keyword with new keyword in file, we have command syntax as
File Path = /tmp/test
Given below is the content. We will replace keyword called example with new keyword called trick .
sharad@linuxworld:~$ cat /tmp/test This is a test file created for vi example. In this example we are learning edit the file with vi without opening it. sharad@linuxworld:~$
Run the command
vi -c "%s/example/trick/g|wq" /tmp/test
See the below given screenshot for more understanding.
You can observe after running the vi command, example keyword is replace with new keyword called trick .