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~2p

What does grep -1 mean? I can find no mention of -1 in grep's man page, or indeed anywhere on the Internet.

  • Would it be grep --count? – Prasanna Apr 3 '16 at 4:13

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.


This is nothing called grep -1, if you go to terminal and type grep --help you don't find grep -1

  • You will find -NUM. So this answer is wrong. – DavidPostill Apr 3 '16 at 10:04
  • Sorry for asking again, let me get this straight. grep -1 is in question yes, but grep -NUM is available not the -1 right? – manjesh23 Apr 3 '16 at 12:35
  • NUM can be replaced by any number, so you are allowed to use -1, -2, -3 ... see the other correct answers. – DavidPostill Apr 3 '16 at 12:37
  • Got you, I have never used grep command hence did man grep to get that. Thank you very much for explaining. – manjesh23 Apr 3 '16 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.