Stand back, I know regular expressions

So, after the last blog post, I was talking to Seveas and mentioned how I thought with all the nifty and useful commands coming in on here, I might make a niftyutils package. The conversation is as follows

<Mez> thinking of compiling a few of these into a nifty-utils package ;)
<Seveas> so we have coreutils, moreutils and nifty-utils?
<Seveas> just contribute them to moreutils :)
<Mez> and a few more
<Mez> patch-utils being the most obvious
<Mez> findutils, psutils …
<Seveas> dennis@mirage:~$ apt-cache -n search utils | wc -l
<Seveas> 266

Ok, so I had to get one up on this, as I only wanted fooutils, not foo-utils or foo-utils-bar, and came up with

apt-cache search utils | awk '{print $1}' | egrep '^.*[^-]utils$' | wc -l

for which I got told off. Awk can do everything, so why pipe to grep?

After a bit of back and forthing, the equivalent command, without egrep is

apt-cache search utils | awk '/^[[:alnum:]]*([-[:alnum:]])*[^-]utils[[:space:]].*/' | wc -l

I think the first version was prettier.

Oh, and another comment relating to my last post, specially going out for “Bork Fomb”:- I’ve deleted your post, and I’m glad I have approval on here - you could have done some damage to people who didn’t know what that does, so here’s a special command, just for you

sudo iptables -I INPUT -s 77.185.71.113 -j DROP

Tags: , , , ,

9 Responses to “Stand back, I know regular expressions”

  1. Jeff Schroeder Says:

    You know regular expressions, but you don’t know that apt supports them!

    I think my version is the prettiest:
    apt-cache search ‘^.*[^-]utils$’ | wc -l

  2. Sebastian Says:

    I cannot resist: why ‘^.*’ in front?
    How about this one:

    apt-cache search ‘[^-]utils$’ | wc -l

  3. Pistahh Says:

    instead of grep … | wc -l you can use grep -c …

    Pistahh

  4. Joe Smith Says:

    I just picked up Mastering Regular Expressions (http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124) for Christmas, and it’s already made my life a million times easier :)

  5. Eike Herzbach Says:

    Stand back, http://partmaps.org/era/unix/award.html#wc ;)

  6. ilmari Says:

    I was going to point out that apt-cache takes regexps, but instead I’ll just mention the –names-only option, which you want in this case (it still searches provides as well as the package name itself, which is why util-linux shows up, as it Provides: schedutils).

  7. Mez Says:

    Eiki, I know I know, but sometimes, you just get lazy

  8. Mez Says:

    Sebastian, as that was the quickest thing to start with - before the expression was refined… yeah, stupid I know.. but, in my experience egrep is generally ungreedy

  9. suy Says:

    Not only apt supports regular expressions directly, but also aptitude has much nicer support for searches. I never ever had to grep the output from aptitude, specially because it’s impossible inside the (optional) GUI mode (and that’s the main reason for aptitude having such a powerful search method).

Leave a Reply