AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1004226
Accepted
Shayan
Shayan
Asked: 2018-02-09 06:05:09 +0800 CST2018-02-09 06:05:09 +0800 CST 2018-02-09 06:05:09 +0800 CST

三个命令的管道[重复]

  • 772
这个问题在这里已经有了答案:
如何在联机帮助页中搜索? (4 个回答)
如何获得有关终端命令的帮助? (9 个回答)
4年前关闭。

在sudo我需要搜索的手册中,-k我尝试了这个:

man sudo | grep '\-k'

但它会导致一段在输出上写一半:

sudo -h | -K | -k | -V
             Similar to the -k option, except that it removes the user's
 -k, --reset-timestamp

相反,我尝试打印整个手册并使用以下颜色为搜索短语着色:

man sudo | grep '\-k\|$'

但由于手册页太长,我无法向上滚动到输出的开头(注意:我不想更改终端配置文件设置中的 Scrollback 行)。

所以我需要使用moreorless命令,才能按页面显示输出,所以我尝试了:

man sudo | grep '\-k\|$' | less

但当然它不起作用,因为管道在第一个命令上运行第二个命令。

知道如何重定向管道吗?

或者grep打印整个段落而不是一行?(这些线基于终端的窗口大小)

command-line pipe
  • 3 3 个回答
  • 153 Views

3 个回答

  • Voted
  1. Best Answer
    Byte Commander
    2018-02-09T06:23:07+08:002018-02-09T06:23:07+08:00

    man默认情况下已less用作寻呼机来显示手册页内容。

    现在less已经有一个内置的搜索和突出显示功能,所以你不需要像这样的外部工具grep。

    man sudo只需在 shell 中运行打开手册页,然后键入/-k(按/并键入您的搜索模式)。less现在将突出显示该正则表达式在文档中匹配的任何位置,并直接跳转到第一个匹配项。

    n然后,您可以通过键入(按)导航到下一个匹配项,N或者通过键入N(按Shift+ N)导航到上一个匹配项。


    更一般地说,如果您想在 中查看grep带有彩色突出显示的输出less,则必须使用grep --color=always(始终发出颜色代码,即使不是输出到终端而是管道) 和less -R(解释颜色转义而不是打印控制字符) :

    man sudo | grep --color=always '\-k\|$' | less -R
    
    • 3
  2. pLumo
    2018-02-09T06:25:02+08:002018-02-09T06:25:02+08:00

    使用grep's-A和switch 捕捉周围的-B线条-C

    man grep | grep '\-[ABC]' -A2
    
       -A NUM, --after-context=NUM
              Print NUM lines of trailing context after matching lines.  Places a line containing a group separator (described under --group-separator) between contiguous groups of matches.  With the -o or
              --only-matching option, this has no effect and a warning is given.
    
       -B NUM, --before-context=NUM
              Print NUM lines of leading context before matching lines.  Places a line containing a group separator (described under --group-separator) between contiguous groups of matches.  With the -o or
              --only-matching option, this has no effect and a warning is given.
    
       -C NUM, -NUM, --context=NUM
              Print  NUM  lines  of  output  context.   Places  a line containing a group separator (described under --group-separator) between contiguous groups of matches.  With the -o or --only-matching
              option, this has no effect and a warning is given.
    

    在你的情况下:

    man sudo | grep '\-k' -A5 -B2
    

    这当然不是完美的,因为您不知道需要多少行,但它仍然可以帮助您找到所需的内容。

    • 3
  3. S.M.
    2018-02-09T06:14:10+08:002018-02-09T06:14:10+08:00

    也许most寻呼机会满足您的要求。尝试

    export PAGER=most
    man sudo
    /-k
    
    • 1

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve