grep1-6 Exercise 2: Required Versus Installed, and a Blank Line ================================================================ (Run in a fresh copy of the practice folder from the chapter. Make it first with the block at the top of the chapter.) printf 'git\ncurl\nvim\ntmux\n\n' > required.txt printf 'curl\nvim\nemacs\nnano\n' > installed.txt echo "required but not installed (required.txt holds a stray blank line at the end):" grep -vFxf installed.txt required.txt | cat -A echo echo "installed but not required, WITHOUT -x (required.txt is the pattern file):" grep -vFf required.txt installed.txt echo "(nothing: the blank pattern line matched every line, so -v removed them all)" echo echo "installed but not required, with -x:" grep -vFxf required.txt installed.txt echo echo "in both, WITHOUT -x:" grep -Ff required.txt installed.txt echo "in both, with -x:" grep -Fxf required.txt installed.txt echo echo "or clean the pattern file instead, and -x stays optional:" grep -v '^$' required.txt > required_clean.txt grep -Ff required_clean.txt installed.txt Output: required but not installed (required.txt holds a stray blank line at the end): git$ tmux$ $ installed but not required, WITHOUT -x (required.txt is the pattern file): (nothing: the blank pattern line matched every line, so -v removed them all) installed but not required, with -x: emacs nano in both, WITHOUT -x: curl vim emacs nano in both, with -x: curl vim or clean the pattern file instead, and -x stays optional: curl vim WHY THIS WORKS AS AN ANSWER --------------------------- 'Required but not installed' searches required.txt and uses installed.txt as the patterns, so the stray blank line is just one more line being searched: it is not in installed.txt, so it is reported along with git and tmux (cat -A shows it as a lone $). It is a nuisance, not a wrong answer. The other two questions use required.txt as the PATTERN file, and there the blank line is dangerous. A blank pattern matches every line, so without -x 'in both' returns everything (curl, vim, emacs and nano: wrong), and 'installed but not required' returns nothing (also wrong). With -x a pattern has to match the whole line, so the blank pattern can only match a blank line, and both answers are right: emacs and nano are installed but not required; curl and vim are in both. Cleaning the file with grep -v '^$' also works. -x is the better habit anyway, because it also stops a required 'vim' from matching an installed 'neovim', which a plain -F would do.