Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 11 Nov 1998 15:58:10 -0800
From:      "Randy A. Katz" <randyk@ccsales.com>
To:        chuck@ucsd.edu, Zdenko Tomasic <zdenko@CS.UH.EDU>
Cc:        fabry@panam.edu, freebsd-questions@FreeBSD.ORG
Subject:   Re: find and replace within files.
Message-ID:  <3.0.5.32.19981111155810.037f54a0@ccsales.com>

next in thread | raw e-mail | index | archive | help
Hi again,

Didn't read your thing and you said directory tree, you could get a list of
the files first:


Hello,

in PERL:

#!/usr/bin/perl
chdir "/directorytostartwith";
@files = system("find . -print");
foreach $file (@files) {
  if (-f "$file") { ### If file is a text file
  open(FILE1,"$file");
  open(FILE2,">$file.tmp");
  while (<FILE1>) {
    s/My Little Text/Replaced With/g;
    print FILE2;
  }
  close(FILE1);
  close(FILE2);
  system("mv $file.tmp $file");
}
This will replace all instances of My Little Text with Replaced With and
write it all out to FILE2 (mynewfile.txt). You can use regular expressions
also in the s///; structure...



At 02:57 PM 11/11/98 -0800, Chuck Rouillard wrote:
>On Wed, 11 Nov 1998, Zdenko Tomasic wrote:
>
>> Take a look at perl. It is probably the most convenient tool for that.
>> (I think it could be done as a 1-liner).
>> 
>> ZT
>> 
>> To Unsubscribe: send mail to majordomo@FreeBSD.org
>> with "unsubscribe freebsd-questions" in the body of the message
>
>Perl or AWK.  Perl especially.
>
>
>
>To Unsubscribe: send mail to majordomo@FreeBSD.org
>with "unsubscribe freebsd-questions" in the body of the message
>
>

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3.0.5.32.19981111155810.037f54a0>