[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Deleting some regexp/simple expression from lots of files in a secure way

On 05/14/2010 10:52 AM, Merciadri Luca wrote:
> "Boyd Stephen Smith Jr." <bss@iguanasuicide.net> writes:
---removed on purpose---
>> (find $dir -type f -exec sed -i -e "s/$regexp//" {} \;) for all files in a 
>> directory.
> Thanks both. (@Andrei: it was in the content of the file, sorry not to
> have specified it before.)
> I am using the second command. The problem is that, for one set of
> files (that I have selected, no problem for this), I have to use a
> really simple expression: I need to find all the occurences of
> `\paragraph{}' and replace them with nothing (i.e. with `'). I know
> regexps, but replacing `$regexp' with `\paragraph{}' gives error
> messages. Any idea? Thanks.

In Boyd's example, you can see 'find' uses '{}' as a place holder, to be
replaced by each file name as it's found.

>From the 'find' man page:

-exec command ;
    Execute  command;  true  if 0 status is returned.  All following
    arguments to find are taken to be arguments to the command until
    an  argument  consisting of ‘;’ is encountered.  The string ‘{}’
    is replaced by the current file name being processed  everywhere
                                         emphasis added:  ^^^^^^^^^^
    it occurs in the arguments to the command, not just in arguments
    where it is alone, as in some versions of find. ...

So, you get file names in both places.  Since file names are probably
relative paths, they will contain added slashes, which confuse sed.

I could not find any reference to how to make '{}' be handled literally
by find.

You could try using a '.*' or '.?' between the braces in the sed
expression and see if that stops find from replacing them.  Since they
both expand to a 'zero or ...', they will match '{}'.  The problem being
they will also match the longest '{...}' in the line, so all lines would
have to have no more than one pair.  As I don't do tex, I don't know if
this can be counted on or not.

sed does have a '-r / --regexp-extended' option, and some 'extended'
regex engines allow specifying a 'shortest' rather than 'longest' match,
but I don't have any experience with extended regex in sed, you will
have to experiment.

Bob McGowan

Reply to: