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

 FreeBSD Tutorials
 Linux LVM Commands
 Free Shell Accounts
 FreeBSD Project
 FreeBSD Handbook
 The OpenBSD Project
 Distrowatch
 FreeBSD Handbook


Apache Webserver Home Page

Blog, intrebari si raspunsuri despre Leasing

Posted on: 15 Feb 2008
Author: mandrei
Section: Server Operating Systems | Administration Guides
Views: 11001
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
» FreeBSD add user command




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.
Verification code
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 - 21: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:
Openssl: Commandline base64 string encoding
Using echo, netcat(nc) and tr to create an HTTP connection
Unix shell - using TR to replace new lines with spaces
SED how to remove multiple white spaces from a string
Starting nginx: [emerg]: directive "rewrite" is not terminated by ";"
Converting PhpBB-SEO Apache RewriteRule to Nginx rewrite
MySql doesn't start: libexec/mysqld: File './mysql-bin.index' not found (Errcode: 13)
/usr/bin/ld: cannot find -lltdl PHP Compiling fails on Centos/Red Hat
How to install apache22 on FreeBSD with proxy modules
Windows XP Microphone volume drops unexpectedly / auto adjusts to low value
 
   Most viewed articles:
How to clear/reset DNS cache on Windows XP / Linux - 15020 views
How to remove first/last character from a string using SED - 14632 views
Reloading /etc/profile - how to reload Unix /etc/profile - 11820 views
Substitute strings in files with perl, sed or vi(m) - 11001 views
How to change a user's password in AIX with the output from ECHO command - 7684 views

   Latest 10 articles:
FreeBSD - Collect installed hard disk drive information - 19 Mar 2009
Set up FTP PROXY via command line in Linux/FreeBSD - 19 Mar 2009
Set up HTTP PROXY via command line in Linux/FreeBSD - 19 Mar 2009
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
qmail qmail-scanner/clamav qmail-inject: fatal: qq temporary problem / clamdscan: corrupt or unknown clamd scanner error or memory/resource/perms problem - exit status 512/2 - 05 Dec 2008
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


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



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