×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Working withText Files

Search and replace text in a file by KevinADC
Posted: 15 Feb 07 (Edited 16 Feb 07)

------------------------------------------------------------
ignore this section:
data
code
perl
regular expression
print
cgi

------------------------------------------------------------

Problem :

I need to search for and replace word(s) or pattern(s) or sentence(s) in a text file (or many text files).

Solution :

Perls in-place editor to the rescue. You can search for and replace just about anything, you are only limited by your ability to write a regular expression (or using other methods) to first find what you are looking for and then replace it with something else, or just remove it.

Here is a bare-bones search and replace script using perls in-place editor.

CODE

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my $filename = 'some_file.txt';
  5. my $find = 'this';
  6. my $replace = 'that';
  7. {
  8.    local @ARGV = ($filename);
  9.    local $^I = '.bac';
  10.    while( <> ){
  11.       if( s/$find/$replace/ig ) {
  12.          print;
  13.       }
  14.       else {
  15.          print;
  16.       }
  17.    }
  18. }
  19. print "Finished";
------------------------------------------------------------
Pragmas (perl 5.8.8) used :
  • strict - Perl pragma to restrict unsafe constructs
  • warnings - Perl pragma to control optional warnings


Discussion :

This is a very simple example. You could use more regular expressions to find more complex patterns to replace or remove. You could use a list of files to edit many files instead of just one file. It could be used as a CGI linked to some form data to tell the script what file(s) to open and search and replace text in.

Code without markup :

CODE

#!/usr/bin/perl

use strict;
use warnings;

my $filename = 'some_file.txt';
my $find = 'this';
my $replace = 'that';

{
   local @ARGV = ($filename);
   local $^I = '.bac';
   while( <> ){
      if( s/$find/$replace/ig ) {
         print;
      }
      else {
         print;
      }
   }
}
 

Back to Perl FAQ Index
Back to Perl Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close