well, blew it... sed or perl q again.

Giorgos Keramidas keramida at ceid.upatras.gr
Tue Dec 30 21:09:04 UTC 2008


On Tue, 30 Dec 2008 12:51:31 -0800, Gary Kline <kline at thought.org> wrote:
> 	All right, then is this the right syntax.  In other words, do
> 	I need the double quotes to match the "http:" string?
>
>   perl -pi.bak -e 'print unless "/m/http:/" || eof; close ARGV if eof' *

Close, but not exactly right...

You have to keep in mind that the argument to -e is a Perl expression,
i.e. something you might type as part of a script that looks like this:

    #!/usr/bin/perl

    while (<STDIN>) {
        YOUR-EXPRESSION-HERE;
        print $_;
    }

One of the ways to print only the lines that do *not* match the
"http://" pattern is:

    print unless (m/http:\/\//);

Note how the '/' characters that are part of the m/.../ expression need
extra backslashes to quote them.  You can avoid this by using another
character for the m/.../ expression delimiter, like:

    print unless (m!http://!);

But you are not still done.  The while loop above already contains a
print statement _outside_ of your expression.  So if you add this to a
perl -p -e '...' invocation you are asking Perl to run this code:

    #!/usr/bin/perl

    while (<STDIN>) {
        print unless (m!http://!);
        print $_;
    }

Each line of input will be printed _anyway_, but you will be duplicating
all the non-http lines.  Use -n instead of -p to fix that:

    perl -n -e 'print unless (m!http://!)'

A tiny detail that may be useful is that "http://" is not required to be
lowercase in URIs.  It may be worth adding the 'i' modifier after the
second '!' of the URI matching expression:

    perl -n -e 'print unless (m!http://!i)'

Once you have that sort-of-working, it may be worth investigating more
elaborate URI matching regexps, because this will match far too much
(including, for instance, all the non-URI lines of this email that
contain the regexp example itself).



More information about the freebsd-questions mailing list