22

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.

3
  • Similar question here Commented Nov 6, 2008 at 9:37
  • As it shows in the tutorial I linked to in my answer, UltraEdit can work with thousands of files, not open in the editor. Commented Nov 8, 2008 at 1:59
  • If you always need those files together, wouldn't it be better to create a single header that includes what is needed, in the order needed? It would be best if the headers could be included in any order - each one should be self-contained and idempotent. And I'd recommend getting rid of the ../ notation too. Commented Nov 26, 2011 at 8:04

13 Answers 13

12

The JetBrains family of IDEs: IntelliJ, RubyMine, PHPStorm etc. will do a multi-line search and replace.

Select the text in question and press ctrl-shift-F -- this will open the replace dialog with the selected text as the find expression (using a regex with newlines escaped as \n)

9

sed will do what you want.

See the FAQ entry about exactly this here http://sed.sourceforge.net/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

3
  • 3
    Not sure what I'm doing wrong but when I run the sed -nf blockrep.sed findrep.txt command to generate the custom.sed file I get the error sed: file blockrep.sed line 18: unterminated 's' command. Looking at the regex on that line ( s,[/\[.*],\\&,g ) it appears correct to me.
    – gene_wood
    Commented Jun 13, 2014 at 17:04
  • just edit the file and move the second [ next to the ] on line 18: s,[/\[.*],\\&,g -> s,[/\.*[],\\&,g the [ character inside a [] group must be next to the close tag ] the ] is the opposite, must be near the open tag [ sed-users.yahoogroups.narkive.com/dKa8Aba3/… Commented Jul 19, 2021 at 15:48
  • stackoverflow.com/a/39280154/311834 Commented Jul 19, 2021 at 15:49
4
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

1
  • 1
    A little explanation would be of great help. Commented Sep 3, 2018 at 2:46
4

Visual Studio Code has an exceptionally good “global” search and replace. I just used it for multiple multi-line changes on numerous files, including some Regexp search with capture and replace.

  • Open VS Code
  • File → Open Folder, open the folder your files reside in
  • Open a file and select the text you want to replace
  • CTRL + SHIFT + H to open Search & Replace
  • If you selected a text it will auto-fill, even multi line. Otherwise enter the text, use SHIFT + Enter to insert newlines, and optionally enable Regexp.
  • Enter the replacement text
  • Press Enter to start the search
  • Click on change-items to preview the changes side by side
  • Use the Replace All button (CTRL + ALT + Enter) to apply and save the changes
3

Only thing that works turnkey in one shot is dreamweaver....enter image description here

2

I successfully used Multiple Find And Replace 1.10, a freeware program that do just that: searches for multiple lines in a TXT or list of TXT's and replace them with other text at your wish or delete them. I found it here for download: http://www.softpedia.com/get/System/File-Management/Multiple-Find-And-Replace.shtml

1
  • Another obsolete answer...
    – Sharcoux
    Commented Sep 17, 2015 at 12:05
2

I'm not allowed to comment, but the accepted solution cites an example that seems broken. Line 18 should be:

s,[/\.*[],\\&,g

Not

s,[/\[.*],\\&,g

The latter will complain with modern sed (or 'gsed' if you're in *BSD) about an unterminated expression on line 18.

1

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

1
  • I wasnt able to use it for recursively replacing in all files of a given type. Recursive doesnt seem to be supported.
    – codingdave
    Commented Aug 28, 2015 at 10:51
1

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.

0

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.

0
0

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.

0

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.

3
  • Another obsolete answer... Links need to be updated. A crossplatform solution would be good
    – Sharcoux
    Commented Sep 17, 2015 at 12:05
  • @Sharcoux Links? Only the one of FileMenu Tools leads to a 404 page, and if you go up two levels in the URL, you will find the product! I call that laziness... Cross-platform: try sed...
    – PhiLho
    Commented Sep 18, 2015 at 14:59
  • FileMenu Tools is obsolete. sed is far from being user friendly when it comes to multiline. Simple Search-Replace doesn't support multiline. So, maybe grepWin could answer the question. I admit that I didn't try it. If you put only grepWin to your answer, I will remove my down vote
    – Sharcoux
    Commented Sep 18, 2015 at 18:47
0
#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 = <FILE>; #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

Not the answer you're looking for? Browse other questions tagged or ask your own question.