`grep -1` 有什么作用?

Sod*_*hty 2 linux shell grep man

我试图理解以下 shell 命令,以获取可以更新的软件包列表:

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
Run Code Online (Sandbox Code Playgroud)

这是什么grep -1意思?-1我在 grep 的手册页或互联网上的任何地方都找不到任何提及。

小智 5

-1选项是传统的 Unix 风格选项,指示您希望在上下文的grep每个匹配中包含1额外的文本行。

如果您执行man grep并查找标题“上下文行控制”,您会发现后面的选项将描述您可以请求额外上下文行的不同方式。具体到你的问题,你会看到:

    -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.
Run Code Online (Sandbox Code Playgroud)

这是记录的地方-1(其中-NUMNUM 为 1),因此以下命令的行为都是相同的:grep -1or grep -C 1orgrep --context=1