FreeBSD - Tutorials, Security
Home   Archives   Sitemap   About   Contact

Substitute strings in files with perl, sed or vi(m)

Home NEW! Unix Forum News 100 Tips and Tricks Website Development Server Operating Systems Databases
 Ivorde.ROarrow Server Operating Systems arrowAdministration GuidesarrowSubstitute strings in files with perl, sed or vi(m) 

Article Sections

    Hello, Guest !
User name:
Password:
 
Google

 SSD VPS Hosting - Vpsie.com
 Mo.nitor.me
 Ivorde forum
 FreeBSD Tutorials
 Linux LVM Commands
 Free Shell Accounts
 FreeBSD Project
 FreeBSD Handbook
 Advanced Bash-Scripting Guide
 The OpenBSD Project
 Distrowatch
 FreeBSD Handbook


Apache Webserver Home Page

Posted on: 15 Feb 2008
Author: mandrei
Section: Server Operating Systems | Administration Guides
Views: 671
Comments: 1 (Add)

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.

Also see How to remove first/last character from a string using SED

Bookmarks: Echo "Substitute strings in files with perl, sed or vi(m)" around:
del.icio.usdiggFurlYahooMyWebGoogleBookmarksFaceBookTechnocratti
-------------------advertising-----------------

Other articles in Server Operating Systems / Administration Guides
» Shell scripting - conditions for IF conditional function
» How to remove first/last character from a string using SED
» How to allow access to su to root on FreeBSD
» Packet Filter broken on FreeBSD 6.2 ? Unusual pf error
» Creating/Adding (temporary) users on FreeBSD 6




Contact webmaster regarding this article
Register or Login to post your article
Hello, Guest ! You can Login or Register to www.ivorde.ro!

 Post comment:

Name:
Title:
Comment:
Please type the word you see in the image (anti-spam verification). Refresh the page if you don't understand the word.
Allowed HTML Tags for comments:<p><strong><em><u><h1><h2><h3><h4><h5><h6><img><li>
<ol><ul><span><div><br><ins><del>

1 comment(s) to Substitute strings in files with perl, sed or vi(m):

1. Re: Substitute strings in files with perl, sed or vi(m)
in base by Guest at May 31st, 2008 - 14:20

One of reasons people use awk or sed is because they are typically in base, while perl is not always (though more and more)

   Latest topics on the forum:
 
   Most viewed articles:
How to copy a mysql database using mysqldump - 10087 views
How to change a user's password in AIX with the output from ECHO command - 9286 views
FreeBSD: Add/remove an additional IP alias - 6453 views
Qmail relay to smarthost: How to route all mail to a smarthost - 4364 views
Change user shell on FreeBSD Linux and AIX - 3118 views

   Latest 10 articles:
Qmail relay to smarthost: How to route all mail to a smarthost - 03 Feb 2009
EXIM 4 relay to smarthost: How to route all mail except local domain - 03 Feb 2009
Windows XP: print LISTEN ports and network connections using netstat - 30 Jan 2009
How to cut out first last n characters from each file name, from a filelist - 04 Nov 2008
Mozilla Firefox3 is now released - 18 Jun 2008
How to switch lower case to upper case and upper case to lower case in a string - 17 Jun 2008
How to rename files/directories to uppercase/lowercase character names - 17 Jun 2008
How to convert lower case to upper case letters in a shell script/command - 17 Jun 2008
Unix,Linux,FreeBSD - How to rename a list of files, replacing spaces inside their names - 12 Jun 2008
How to change a user's password in AIX with the output from ECHO command - 21 May 2008


Archives
» 2007  |  June  |  October  |  November  |  December
» 2008  |  January  |  February  |  March  |  April  |  May  |  June  |  November
» 2009  |  January  |  February



Home | Archives | Sitemap | About | Contact

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.
Valid W3 Document Valid XHTML 1.0 Transitional Valid CSS! The FreeBSD Project Viewable With Any Browser