Substitute strings in files with perl, sed or vi(m) substitute strings in files with perl, substitute strings in files with sed, substitute strings in files with vim
Substitute strings inside a file
Let's say we have the following file:
$ cat file FreeBSD is a very secure operating system. Unix systems are more stable and resource efficient operating systems. DragonFlyBSD is derived from FreeBSD. Unix is unisex. $
And we want to substitute FreeBSD with Windows, for example. We have at least 3 methods:
Substitute strings inside a file with PERL
$ perl -p -i -e "s/FreeBSD/Windows/" file $ cat file Windows is a very secure operating system. Unix systems are more stable and resource efficient operating systems. DragonFlyBSD is derived from Windows. Unix is unisex.
The substitution command with PERL, in this case, is perl -p -i -e "s/FreeBSD/Windows/" file ([ -i[extension] ] [ -e 'command' ]) where: - file is the file in which we want to substitute the strings. - FreeBSD is the string which we want to substitute (old string) - Windows is the string which we want to substitute with (new string)
Substitute strings inside a file with SED
Now we will substitute back - Windows with FreeBSD
$ sed -i -e 's/Windows/FreeBSD/' file $ cat file FreeBSD is a very secure operating system. Unix systems are more stable and resource efficient operating systems. DragonFlyBSD is derived from FreeBSD. Unix is unisex.
In the second example, the sed substitution command is sed -i -e 's/Windows/FreeBSD/' file (-i extension -e command) and similar to the first example, perl substitution command, Windows is the substituted string, while FreeBSD is the new string.
To remember that this second example works on FreeBSD sed and Linux sed. AIX sed doesn't support these options.
Substitute strings inside a file with VI or VIM
$ vim file FreeBSD is a very secure operating system. Unix systems are more stable and resource efficient operating systems. DragonFlyBSD is derived from FreeBSD. Unix is unisex.
file 1,1 All :%s/Unix/Unix OS/g
################################ (IT BECOMES) FreeBSD is a very secure operating system. Unix OS systems are more stable and resource efficient operating systems. DragonFlyBSD is derived from FreeBSD. Unix OS is unisex.
In the above example I edied file with VIM editor and, inside VIM, in normal mode (not edit mode), I ran the command %s/Unix/Unix OS/g (In vi/vim you need to type : in normal mode to enter a command). This command substituted Unix string with Unix OS.
Remember that:
1. You'll need to escape special characters with a backslash () for example space: Unix/Unix OS/
2. Substituting strings with perl as in 1st examples works on most Unix systems. The sed method works (from what I've tested) on Linux and BSD only. And the third method, using vi/vim works on all unix systems.
Designed and developed by Andrei Manescu. Optimized for Mozilla Firefox.
Copyright 2007 Andrei Manescu
All trademarks and copyrights on this page are owned by their respective owners. Comments are owned by those who posted them.