I'm trying to make sense of the following shell command, to get a list of packages that can be updated:
dpkg --get-selections | xargs apt-cache policy {} | grep -1 Installed | sed -r 's/(:|Installed: |Candidate: )//' | uniq -u | tac | sed '/--/I,+1 d' | tac | sed '$d' | sed -n 1~2pWhat does grep -1 mean? I can find no mention of -1 in grep's man page, or indeed anywhere on the Internet.
3 Answers
the -1 option is the traditional Unix style option to indicate that you want grep to include 1 line of extra text per match for context.
If you do a man grep and look for the heading Context Line Control you find the options that follow will describe the different ways you can ask for extra lines of context. Specifically as to your question you'll see:
-C NUM, -NUM, --context=NUM Print NUM lines of output context. Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.this is where the -1 is documented (as -NUM where NUM is 1) so the following commands would all behave the same: grep -1 or grep -C 1 or grep --context=1
I managed to work it out by looking at the results. It specifies how many preceding and succeeding lines (of "context") to show for each match. So in this particular case, grep will show the previous line and the next line for each match, in addition to the matching line itself.
2This is nothing called grep -1, if you go to terminal and type grep --help you don't find grep -1