brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: look finds lines that start with a word in a sorted file, right in the terminal

Terminal
~/tips/tip-terminal-look-prefix-search

look recurs

Searches a sorted list by prefix, fast, and doubles as a spelling helper.

Not sure how to spell a word in a commit message? Your Mac and many Linux systems have a word list and an old tool that searches it by prefix.

Without a file, look searches /usr/share/dict/words, ignoring case:

look recurs
recurse
recursion
recursive

Give it a file and it prints every line that begins with your string. Because it does a binary search instead of reading the whole file, it stays quick on huge sorted lists, like an exported list of host names or a big sorted log index:

sort -o hosts.txt hosts.txt
look -f web hosts.txt

-f ignores case, and -t stops comparing at a character you name. The exit status is 0 when something matched and 1 when nothing did, so it works in an if too.

The spelling trick is its best use, I think, and it can also check if a name is already in a long sorted list.

The file really has to be sorted, and sorted the same way look compares. On an unsorted file it doesn’t complain, it just misses lines that are there, which is worse than an error. With -f, sort with sort -f as well. The word list is not installed on every Linux system; on Debian and Ubuntu it comes with a package such as wamerican.

Further reading: look(1) Linux manual page, look man page (macOS)

More Terminal tips ioreg -rn AppleSmartBattery | grep CycleCount Terminal can save your windows and tabs as a window group Export a Terminal profile to a .terminal file and import it on another Mac mc hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000029}]}' All Terminal tips (116) All topics

Link