On Fri, 30 Jul 2004, Andrew Gaffney wrote:

I need to get a list of all the files that end with '.html' in a directory and all of its subdirectories. I then want to search through each file and remove the ones from the list that contain '<%perl>' or '<%init>'. How can I do this? Thanks for any help.

From a Unix command line, you could do something like this:

$ find /path/to/htdocs -type f | xargs egrep -li '<%(perl|init)>'

The above line results in a list of all the files that have either '<%perl>' or '<%init>' in them.

From here, you can o a step further by deleting them all. Because files
with spaces in their name (or their path) can break this horribly, I'll use `sed` to wrap each line in quotes before removing them:

    $ find /path/to/htdocs -type f | \
    > xargs egrep -li '<%(perl|init)>' | \
    > sed 's/\(.*\)/"\1"/' | \
    > xargs rm -i

This should also prompt you before taking any action, in case you realize that you really wanted one of these files. If you want to just proceed blindly -- and my but you're brave if you do -- then delete the "-i" from the last line.


Of course, you probably wanted to do this in Perl, but sometimes things are just as easy to do with shell tools, and this seems like a good example. Unless you want to do this all the time -- in which case go ahead & script it in Perl -- a shell one liner like this should be fine.




And of course this all breaks down if you're using Windows, in which case unless you're a fan of Cygwin you can just ignore all of this :)



--
Chris Devers

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to