views:

3477

answers:

11

I am looking for a tool to replace multiple lines through out a project. For example:

#include "../DiscreteIO/Discrete.h"
#include "../PCI/pci.h"
#include "../Arinc429/ARINC429.h"

with

#include "../PCI/pci.h"
#include "../DiscreteIO/DiscreteHW.h"
#include "../DiscreteIO/Discrete.h"

I have tried two tools that work for this type of search and replace. Wildedit and Actual search and replace Both seem to be excellent tools but are shareware. Do anybody know of similar tools? Anything free or is it time to part with some money?

Clarification:

through out a project in this case means a thousand plus c files. The text editors can do this only one file at a time (Textpad, Programmers notepad) or in all open files(nodepad++). I haven't tried any of the other editors but I assume they will have similar problems. Please correct me if I am wrong.

Tools like sed & awk is a solution but present problems since I do not use them regularly and need to spend some time getting something to work since I am not a expert on the tools.

The answer is: All of it...

Ultra edit can work but I already have an editor and the price is steep if I am just going to use it as a search and replace tool.

Sed, AWK and regular expression based tools can work but can be a pain in some cases.

Wild Edit can work and is not that expensive.

My decision in the end is to work my Regular expression skills.

+1  A: 

Ultraedit can do that. I parted with money for it.

There is a tutorial on multi-line find & replace which you can use with "replace in files" to apply over multiple files.

Stephen Denne
A: 

I'm pretty sure a combination of sed, awk and/or grep would do the job. They're free and come with any Linux distribution but Windows versions exist as well.

Yann Semet
A: 

Similar question here

Stephen Denne
+5  A: 

sed will do what you want.

See the FAQ entry about exactly this here http://www.student.northpark.edu/pemente/sed/sedfaq4.html#s4.23.3

If you need to match a static block of text (which may occur any number of times throughout a file), where the contents of the block are known in advance, then this script is easy to use

Sed is available for Windows. See http://gnuwin32.sourceforge.net/packages/sed.htm

Paul
A: 

I am always perplex when people ask for tools without telling for which platform(s) they need them... In these cases, I suppose it is, by default, for Windows...

There are a number of search/replace tools available for Windows, more or less powerful, lot of them free.
Currently, I use the Search/Replace module of FileMenu Tools, surprisingly powerful (you can use regular expressions), coming with lot of other goodies.
I have also RJL Soft's Simple Search-Replace (no REs), TortoiseSVN author's grepWin, supporting regexes and a good number of options, and there are some others I used in the past.

PS.: I forgot. Simple Search-Replace won't do multi-line replaces. Neither FileMenu Tools, apparently. grepWin can do (using \r\n for example). I do recall such free tools with multi-line search/replace fields, but I don't have their names at hand.

PhiLho
A: 

RegexSearch is a bit ugly but a very useful tool for just this sort of thing.

+1  A: 

May be Multiple Find And Replace 1.00

Multiple Find And Replace

Javier Suero Santos
A: 

For a windows platform, I can second the UltraEdit-32 suggestion given above - it is a wonderful tool, and well worth the money. Other then that, I think your best bet is going to either be a small perl script, or else awk/sed, again as mentioned above.

Lokkju
A: 

The best tool I've found is a piece of freeware called BK ReplaceEm. The interface takes a bit of getting used to, but it's worth learning how to use it. The tool is very powerful and allows you to do multi-line replaces with or without regular expressions.

A: 

see docs at the bottom

use strict; use warnings; use Cwd;

use File::Find;

my $search_patternFilePath=$ARGV[0] ; my $replace_patternFilePath =$ARGV[1]; my $file_pattern = $ARGV[2];

Usage

(@ARGV == 3 ) || die ("Usage: FindAndReplace.pl pathToFileContaingTheMultiLineSearchText FullPathToFileContainingMultiLineReplaceText FilePattern . Example: perl MultiLineFindAndReplace.pl \"D:\Opera\New Folder\search.txt\" \"D:\Opera\replace.txt\" bak");

find(\&d, cwd);

sub d { my $file = $File::Find::name; $file =~ s,/,\,g;

return unless -f $file; return unless $file =~ /$file_pattern/;

my $searchPatternString = &slurpFile ( $search_patternFilePath ) ; my $replacePatternString = &slurpFile ( $replace_patternFilePath ) ; my $fileStr = &slurpFile ( $file ) ;

$fileStr =~ s/$searchPatternString/$replacePatternString/igo ; open(FILEHANDLE,">$file") || die "cannot open output file"; print (FILEHANDLE "$fileStr"); close FILEHANDLE ;

}

sub slurpFile { my $file = shift ; print "\$file is $file" ; local( $/, *FILE ) ; open (FILE , $file) or die "Cannot find $file !!! " ; my $fileString = ; #slurp the whole file into one string !!! close FILE ; return $fileString ; }

Purpose : performs recursive find and replace based on pеrl regexes from the current directory

the search and replace is case insensitive

Usage

perl MultiLineFindAndReplace.pl "D:\Opera\New Folder\search.txt" "D:\Opera\replace.txt" bak

YordanGeorgiev
+1  A: 
cd $PROJECTDIR && find . -iname '*.*' -exec vim -c 's:^first line of text\nsecond line of text\n3rd line of text:new 1st line\rnew 2nd\rnew 3rd:' -c 'w!' -c 'q' {} \;

HTH

Zsolt Botykai