What does `grep -1` do?

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.

2

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.

2

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

4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like