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: awk, egrep, gawk, grep, regular expressions
January 3rd, 2008 at 3:38 am
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
January 3rd, 2008 at 7:15 am
I cannot resist: why ‘^.*’ in front?
How about this one:
apt-cache search ‘[^-]utils$’ | wc -l
January 3rd, 2008 at 8:13 am
instead of grep … | wc -l you can use grep -c …
Pistahh
January 3rd, 2008 at 8:57 am
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
January 3rd, 2008 at 12:19 pm
Stand back, http://partmaps.org/era/unix/award.html#wc
January 3rd, 2008 at 5:32 pm
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).
January 3rd, 2008 at 8:49 pm
Eiki, I know I know, but sometimes, you just get lazy
January 3rd, 2008 at 8:50 pm
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
January 3rd, 2008 at 11:14 pm
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).