Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 26 Jan 1997 20:41:52 -0800 (PST)
From:      "Bryan K. Ogawa" <bkogawa@primenet.com>
To:        robert@nanguo.chalmers.com.au
Cc:        freebsd-questions@freebsd.org (bsd)
Subject:   Re: how do I get sed to search sundirs?
Message-ID:  <199701270441.UAA11606@foo.primenet.com>
References:  <199701270326.NAA04282@nanguo.chalmers.com.au>

next in thread | previous in thread | raw e-mail | index | archive | help

In localhost.freebsd.questions you write:

>I have a little sed script that will search through all files in a
>directory, replacing one phrase with another, and writing it back.
>How do I get it to step down through all the sub-directories as well?

Hm.  I would do it in perl, like this:

  #!/usr/bin/perl

  require "find.pl";

  &find(".");

  sub wanted {
      if($_ =~ /\.html$/) {
	  rename $_, "$_.bak";
	  open(IN, "$_.$$.bak");
	  open(OUT, ">$_");
	  while(<IN>) {
	      s/OLDSTRING/NEWSTRING/g;
	      print OUT;
	  }
	  close(IN);
	  close(OUT);
      }
  }

This will do a search and replace of OLDSTRING with NEWSTRING with
every file ending in .html in the tree starting with the current
directory, first making a backup of each .html file to .html.bak .

I have not tested this, though, so please take it with a grain of
salt.


If you want to call some script on every file in a directory (and on
all of the subdirectories), you can also do this from perl:

  #!/usr/bin/perl

  require "find.pl";
  &find(".");

  sub wanted {
      if (-f $_) {
	 system("/path/to/script $_");
      }
  }

This will call /path/to/script   once on every plain file in the tree
rooted at . .

I hope this helps.

# Usage:
#       require "find.pl";
#
#       &find('/foo','/bar');
#
#       sub wanted { ... }
#               where wanted does whatever you want.  $dir contains the
#               current directory name, and $_ the current filename within
#               that directory.  $name contains "$dir/$_".  You are cd'ed
#               to $dir when the function is called.  The function may
#               set $prune to prune the tree.
#

>ta
>bob
>-- 
>Triple-W: P.O. Box 2003. Mackay. 4740       		   +61-0412-079025
>robert@chalmers.com.au for Whirled Peas		http://www.chalmers.com.au
>Location: Whitsunday Web Works.			21'7" S, 149'14" E.


-- 
bryan k ogawa  <bkogawa@primenet.com>   http://www.primenet.com/~bkogawa/



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