Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Replace all occurrences of a string

Reply
Thread Tools

Replace all occurrences of a string

 
 
Shabam
Guest
Posts: n/a
 
      11-16-2004
Is there a utility or command that will allow me to replace all occurrences
of a string within all files in a directory?

I'm trying to mass update IP addresses of all DNS zone files in my
/var/named/ folder.


 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      11-17-2004
In article <(E-Mail Removed)>, Shabam
<(E-Mail Removed)> wrote:

> Is there a utility or command that will allow me to replace all occurrences
> of a string within all files in a directory?
>
> I'm trying to mass update IP addresses of all DNS zone files in my
> /var/named/ folder.


You can use a Perl one-liner of the type

perl -pi'bak' -e 's/bar/baz' /var/named/*

See 'perldoc perlrun' for more options. Try it out on some test files
first!
 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      11-17-2004
Jim Gibson wrote:
> In article <(E-Mail Removed)>, Shabam
> <(E-Mail Removed)> wrote:
>
>
>>Is there a utility or command that will allow me to replace all occurrences
>>of a string within all files in a directory?
>>
>>I'm trying to mass update IP addresses of all DNS zone files in my
>>/var/named/ folder.

>
>
> You can use a Perl one-liner of the type
>
> perl -pi'bak' -e 's/bar/baz' /var/named/*
>
> See 'perldoc perlrun' for more options. Try it out on some test files
> first!


You'll need the closing slash and probably the g modifier on that s///.

perl -pi.bak -e 's/oldtext/newtext/g' /var/named/*

More sophisticated stuff with /e.

perl -pi -e 's/(10\.0\.)(\d+)(\.\d+)/$1.($2+100).$3/e' /var/named/*

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting Occurrences of a String in an Array shrouded.shoyu@gmail.com Ruby 4 02-10-2009 10:09 PM
Beginners: Count occurrences of a string within a string yogi_bear_79 C++ 9 02-28-2008 09:14 PM
How can I replace all occurrences of a character with another character in std string? herman C++ 5 08-30-2007 09:05 AM
how to remove multiple occurrences of a string within a list? bahoo Python 37 04-09-2007 12:09 AM
count the number of occurrences of a substring in a string sibingpeter@gmail.com C Programming 4 10-02-2005 07:37 PM



Advertisments