Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search and replace text in a file

Working withText Files

Search and replace text in a file

by  KevinADC  Posted    (Edited  )
------------------------------------------------------------
[small] ignore this section:
data
code
perl
regular expression
print
cgi[/small]
------------------------------------------------------------

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:
[ol]
[li][gray]#!/usr/bin/perl[/gray][/li]
[li][/li]
[li][link http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/link] [green]strict[/green][red];[/red][/li]
[li][black][b]use[/b][/black] [green]warnings[/green][red];[/red][/li]
[li][/li]
[li][link http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/link] [blue]$filename[/blue] = [red]'[/red][purple]some_file.txt[/purple][red]'[/red][red];[/red][/li]
[li][black][b]my[/b][/black] [blue]$find[/blue] = [red]'[/red][purple]this[/purple][red]'[/red][red];[/red][/li]
[li][black][b]my[/b][/black] [blue]$replace[/blue] = [red]'[/red][purple]that[/purple][red]'[/red][red];[/red][/li]
[li][/li]
[li][red]{[/red][/li]
[li]   [link http://perldoc.perl.org/functions/local.html][black][b]local[/b][/black][/link] [blue]@ARGV[/blue] = [red]([/red][blue]$filename[/blue][red])[/red][red];[/red][/li]
[li]   [black][b]local[/b][/black] [blue]$^I[/blue] = [red]'[/red][purple].bac[/purple][red]'[/red][red];[/red][/li]
[li]   [olive][b]while[/b][/olive][red]([/red] <> [red])[/red][red]{[/red][/li]
[li]      [olive][b]if[/b][/olive][red]([/red] [red]s/[/red][purple][blue]$find[/blue][/purple][red]/[/red][purple][blue]$replace[/blue][/purple][red]/[/red][red]ig[/red] [red])[/red] [red]{[/red][/li]
[li]         [link http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/link][red];[/red][/li]
[li]      [red]}[/red][/li]
[li]      [olive][b]else[/b][/olive] [red]{[/red][/li]
[li]         [black][b]print[/b][/black][red];[/red][/li]
[li]      [red]}[/red][/li]
[li]   [red]}[/red][/li]
[li][red]}[/red][/li]
[li][black][b]print[/b][/black] [red]"[/red][purple]Finished[/purple][red]"[/red][red];[/red][/li]
[/ol]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li][link http://perldoc.perl.org/strict.html]strict[/link] - Perl pragma to restrict unsafe constructs[/li]
[li][link http://perldoc.perl.org/warnings.html]warnings[/link] - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]

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;
      }
   }
}
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top