Replace all occurrences of a string

S

Shabam

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.
 
J

Jim Gibson

Shabam said:
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!
 
J

Joe Smith

Jim said:
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/*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top