[wellylug] Is there a good way to....

Grant McLean grant at mclean.net.nz
Thu Jan 29 20:54:32 NZDT 2004


Andrej Ricnik wrote:
> On Thu, 29 Jan 2004 18:21, Ewen McNeill wrote:
> 
>> grep -v
>> Perhaps I don't understand your problem enough.
> 
> I think he wants to do that to the same file ...
> 
> so he'd have to output grep to a tmp-file and
> then move the result (via script?) to the original.

This thread would not be complete without a Perl solution :-)
So here are two.  One that works and one that doesn't:

   perl -i -n -e 'print unless /pattern/ || /pattern/' filenames

The -i does the in-place editing of the files

The -n causes the code specified with -e to be executed for each line in 
each input file.

I initially tried this:

   perl -i -p -e 'next if /pattern/ || /pattern/' filenames

But it didn't work because the -p is like -n except it adds a print in a 
continue block - and continue blocks get run even on a next.

Grant





More information about the wellylug mailing list