Tag Archives: sort

alot: With notmuch help involved, and little more

Up front, let’s say that alot is a “graphical” interface to notmuch, which you might know or remember as a mail indexer and search tool.

2015-04-14-6m47421-alot-01 2015-04-14-6m47421-alot-02 2015-04-14-6m47421-alot-03

And as you can see, alot has a lot πŸ˜› of the right stuff — good color, very flexible terminal space, on-screen help and an intuitive interface. Navigation is with enter keys and a few other mnemonics, and some keys have global behaviors, regardless of what you’re doing at any moment in time.

I would bet that, once your mail setup is complete, you’ll only need about three or four minutes to learn how to use alot, and after that, you’ll cruise through your mail folder at ludicrous speed.

A few things to remember though. … alot needs notmuch on board, which means you’re dealing mostly with the local mail concept, as opposed to relaying directly to an online mail source. If you’re looking for something that will pull down messages from GMail and let you pick through them, I don’t think this is it.

On the other hand, it does mean that you’re free to latch alot (and notmuch) on to your home-grown mail system — or as I showed above, latch notmuch onto something like offlineimap, which can yank messages off GMail and store them locally.

So technically speaking, there are options. The question becomes, how much do you want to step backwards through programs, just to take advantage of alot, when things like alpine can more or less handle the entire process in one fell swoop? Again, if you use a local mail system, alot might be preferable. For me … well. … 😐

All in all, I have to give alot a gold star for catching every point in my checklist for a good console application. I don’t think this is a game-changer for me though, since I’d have to rely on two or three other programs before alot became my mail reader, even if setup is fairly straightforward for all of them.

In AUR as alot and alot-git, but the git version wasn’t working at this exact moment in time. In Debian. And of course, as promised: ⭐ Enjoy! πŸ™‚

uniq: The unique solution

Everybody knows about sort, but nobody seems to know about uniq. More’s the pity, since uniq takes sort‘s product and does still cooler things with it.

Consider: A list of 10,000 supposedly random words. They’re scrambled and it’s difficult to see where words are repeated. How can we find out how many words are duplicates, and how many times?

Easier done than said, if you have uniq. uniq works best with sorted lists — actually, uniq doesn’t work very well at all without sorted lists — so let’s sort our list first.

sort test.txt > sorted.txt

Next, we fire up uniq. We want to know how many of each word, and it would be nice if we could see the highest numbers first, rather than at the end of the list. And actually, just the first 20 would be enough to satisfy our curiosity. Ergo,

uniq -d -c sorted.txt | sort -r | head -20

Results?

2014-06-10-6m47421-uniq

The -d flag plucks out repeated words, rather than just listing everything (its opposite is -u, which shows only singletons). -c adds a line count to the front. We pipe it back through sort -r so we can reverse the output, and head just cuts off the list after the first 20. Simple, huh? πŸ™‚

“That’s not so special,” you say. “Why not just use sort -u, K.Mandla. Duh.”

Because, duh, sort -u doesn’t show duplicated lines. It sorts the output and removes duplicate lines. So not only don’t you get the output you want, but you’ve hopelessly trashed your data file, because the duplicated entries have vanished. Duh. 😯

uniq has a few other options that will help you get the results you want. It’s particularly useful for finding similar names in lists, or comparing the contents of different directories. List them both, sort the mixed results, and pull out the uniq‘s.

And where, pray tell, might one find this marvel of modern programming? In coreutils, of course. πŸ˜‰

sort: Deserves better attention

A long time ago, when I was assembling The List, I thought to myself that I should save a little extra time and space for sort. Unfortunately it’s not going to work out, because of some time crunches I have in real life. And that’s a shame, because in no small sense, it’s a really great tool that saves me a lot of hassle.

Here’s an example. What’s your machine say if you ask it this?

lsmod

If it’s anything like mine, and it probably is, you get a huge smattering of modules that are currently inserted into your kernel. That’s what it’s supposed to say.

Now try to pick out the ones that are interrelated. Not so easy, is it? If, for example, I want to see if the ath5k module was inserted when I jammed a PCMCIA wireless card in, sort comes to my rescue.

lsmod | sort

Yes, I know I could use grep, and in some cases I would. But modules tend to be interrelated, and sometimes it’s more useful to have a full list to scan, especially when troubleshooting.

sort can do a couple of interesting things. The -u flag will avoid doubling-up on entries, if you’re not interested in duplicates. The -h flag, which allows sorts by human-readable numeric values. How is that useful?

ls /etc -hs | sort -h

Now it’s useful. Even better, here’s the top five biggest files in a directory.

ls /etc -hs | sort -h -r | head -5

There are many ways to do that; that’s just one way to skin the proverbial cat.

sort gets a little cryptic when you’re not interested in sorting by the first character in a line, but it’s not impossible. Here’s a deliberately screwy text file, tab-separated, and we’re going to sort by the second column. 😯 Trust me. πŸ˜‰

kmandla@6m47421: ~$ for i in {1..10} ; do echo -e $(shuf -n 1 /usr/share/dict/cracklib-small )"\t"$(shuf -n 1 /usr/share/dict/cracklib-small ) >> test.txt ; done

kmandla@6m47421: ~$ cat test.txt | column -t
sial         nullstellensatz
galloped     codicil
pored        presence
measurer     lane
protective   ocean's
rapport      scotsmen
shrewd       sift
calculation  drafted
parklike     gimmicks
moslem       logo

The trick is to use the field separator and key flags to tell sort to look for a tab, and to sort by the first characters after that.

kmandla@6m47421: ~$ sort -t$'\t' -k2 test.txt | column -t
galloped     codicil
calculation  drafted
parklike     gimmicks
measurer     lane
moslem       logo
sial         nullstellensatz
protective   ocean's
pored        presence
rapport      scotsmen
shrewd       sift

The $'\t' represents our tab character, and the -k2 tells sort to do its magic on the second field it finds. Voila. That wasn’t so hard, was it?

I had a few other things I hoped to show with sort, but honestly, time these days is very short. Maybe one day we can come back and swap sort war stories. … πŸ˜€

P.S.: sort, like all great command line tools, is part of coreutils. πŸ˜‰

reflector: One more for Archers

In the interest of parity, and since there have been a lot of Debian-only posts in the past, here’s reflector — an Arch-only trick.

2014-04-13-6m47421-reflector-uk

Mirror management is usually an easy-to-forget, one-time task when building a system, but it might be worth keeping reflector in mind.

I’ve used rankmirrors plenty of times, and if there’s no other available option, it does a fine job. But rankmirrors does expect you to do a little background work, and at times can be a bit time-consuming. All of which is easy to work around, of course.

reflector, in my humble opinion, has the added bonus of being able to filter mirrors by geographical area, which is great if you’re a world traveler and want to update between stopovers.

Or it might just be that some of the mirrors rankmirrors gave you are sluggish or remote, in which case reflector might have a few better ideas for you.

And of course, the best place to learn about reflector is on the one-and-only Arch wiki, which is only the best source for Linux information in the universe. Regardless of your distro. πŸ˜‰