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: grep -vxFf shows which lines of one list are missing from another

Linux
~/tips/tip-linux-grep-vxff-compare-lists

grep -vxFf allowed.txt users.txt

Compare two lists without sorting them first, line for line and exact.

To find which names in one list are not in another, comm works, but only on sorted input. A combination of grep options works on lists in any order.

grep -vxFf allowed.txt users.txt

It reads as four options. -f allowed.txt takes every line of that file as a pattern. -F treats those patterns as plain text, so dots and other special characters match literally. -x only matches when the whole line is equal, so bob does not match bobby. And -v turns it around: print the lines of users.txt that match none of the patterns.

So with alice and bob in the allowed list, and alice, bobby and carol in the user list, the output is bobby and carol. I think this kind of check is good for finding accounts or SSH keys that are not on the list they should be on.

Leave out -v and you get the lines that do appear in both.

An empty line in the pattern file is a trap. An empty pattern matches every line, so with -v the result is suddenly empty and it looks like everything is fine. Remove blank lines from the pattern file first, for example with grep -v '^$'. Windows line endings cause the opposite problem: nothing matches at all.

Further reading: GNU grep manual: matching control

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link